Get rid of the memory allocation wrappers

This commit is contained in:
Pierre Pronchery 2020-04-11 06:02:10 +02:00
parent a434a20075
commit 26ff7d0813
4 changed files with 3 additions and 83 deletions

View File

@ -1,67 +0,0 @@
/* $Id$ */
#include <stdlib.h>
#include <stdio.h>
#include "common.h"
/* useful */
/* g_alloc */
void * g_alloc(size_t size)
{
size_t i;
void ** p;
for(i = 0; i < gt.alloced_cnt; i++)
if(gt.alloced[i] == NULL)
break;
if(i == gt.alloced_cnt)
{
if((p = realloc(gt.alloced, sizeof(void*) * (i+1))) == NULL)
return NULL;
gt.alloced = p;
gt.alloced_cnt++;
}
gt.alloced[i] = malloc(size);
return gt.alloced[i];
}
/* g_alloced */
void * g_alloced(void * ptr)
{
size_t i;
for(i = 0; i < gt.alloced_cnt; i++)
if(gt.alloced[i] == ptr)
return ptr;
return NULL;
}
/* g_free */
void g_free(void * ptr)
{
size_t i;
void ** p;
for(i = 0; i < gt.alloced_cnt; i++)
if(gt.alloced[i] == ptr)
break;
if(i == gt.alloced_cnt)
{
fprintf(stderr, "%s%p\n", "Unknown address to free: ", ptr);
return;
}
free(ptr);
gt.alloced[i] = NULL;
if(i != gt.alloced_cnt-1)
return;
for(; i > 0 && gt.alloced[i-1] == NULL; i--);
if((p = realloc(gt.alloced, sizeof(void*) * i)) == NULL)
return;
gt.alloced = p;
gt.alloced_cnt = i;
}

View File

@ -13,10 +13,6 @@
/* types */
typedef struct _GToolkit
{
/* memory */
void ** alloced;
size_t alloced_cnt;
/* main loop */
int loop;
@ -29,11 +25,4 @@ typedef struct _GToolkit
/* variables */
extern GToolkit gt;
/* useful */
/* FIXME memory management useful for users of the library as well? */
void * g_alloc(size_t size);
void * g_alloced(void * ptr);
void g_free(void * ptr);
#endif

View File

@ -29,7 +29,7 @@ GWindow * gwindow_new(void)
GWindow * gwindow;
XSetWindowAttributes attr;
if((gwindow = g_alloc(sizeof(*gwindow))) == NULL)
if((gwindow = malloc(sizeof(*gwindow))) == NULL)
return NULL; /* FIXME report */
/* FIXME colormap defined in g_init()? */
attr.colormap = XCreateColormap(gt.display,
@ -51,10 +51,8 @@ GWindow * gwindow_new(void)
/* gwindow_delete */
void gwindow_delete(GWindow * gwindow)
{
if(g_alloced(gwindow) != gwindow)
return;
glXDestroyContext(gt.display, gwindow->context);
g_free(gwindow);
free(gwindow);
}

View File

@ -9,4 +9,4 @@ type=library
cflags=`pkg-config --cflags gl`
ldflags=`pkg-config --libs gl`
install=$(PREFIX)/lib
sources=common.c,gwindow.c,gtoolkit.c
sources=gwindow.c,gtoolkit.c