Remove memory wrappers, and use system stuff directly.

This commit is contained in:
Qball Cow 2014-05-19 16:48:50 +02:00
parent b535533569
commit cb15a57fb7
7 changed files with 32 additions and 94 deletions

View file

@ -41,15 +41,6 @@ MenuReturn menu ( char **lines, char **input, char *prompt,
menu_match_cb mmc, void *mmc_data,
int *selected_line );
/**
* Allocator wrappers
*/
void* allocate ( unsigned long bytes ) __attribute__( ( malloc ) );
void* allocate_clear ( unsigned long bytes );
void* reallocate ( void *ptr, unsigned long bytes );
void catch_exit ( __attribute__( ( unused ) ) int sig );
typedef enum _WindowLocation

View file

@ -46,7 +46,7 @@ static char **get_dmenu ( void )
while ( fgets ( buffer, 1024, stdin ) != NULL )
{
retv = reallocate ( retv, ( index + 2 ) * sizeof ( char* ) );
retv = realloc ( retv, ( index + 2 ) * sizeof ( char* ) );
retv[index] = strdup ( buffer );
retv[index + 1] = NULL;

View file

@ -90,8 +90,8 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
{
continue;
}
retv = reallocate ( retv, ( *length + 2 ) * sizeof ( _element* ) );
retv[(*length)] = allocate ( sizeof ( _element ) );
retv = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
retv[(*length)] = malloc ( sizeof ( _element ) );
// remove trailing \n
buffer[strlen ( buffer ) - 1] = '\0';
// Parse the number of times.
@ -139,8 +139,8 @@ void history_set ( const char *filename, const char *entry )
}else{
// If not exists, add it.
// Increase list by one
list = reallocate(list,(length+2)*sizeof(_element *));
list[length] = allocate(sizeof(_element));
list = realloc(list,(length+2)*sizeof(_element *));
list[length] = malloc(sizeof(_element));
// Copy name
strncpy(list[length]->name, entry, HISTORY_NAME_LENGTH);
list[length]->name[HISTORY_NAME_LENGTH-1] = '\0';
@ -247,7 +247,7 @@ char ** history_get_list ( const char *filename, unsigned int *length )
// Copy list in right format.
if((*length) > 0 )
{
retv = allocate(((*length)+1)*sizeof(char *));
retv = malloc(((*length)+1)*sizeof(char *));
for ( int iter = 0; iter < (*length); iter++)
{
retv[iter] = strdup(list[iter]->name);

View file

@ -104,59 +104,6 @@ int token_match ( char **tokens, const char *input,
return match;
}
void* allocate ( unsigned long bytes )
{
if ( bytes == 0 )
{
return NULL;
}
void *ptr = malloc ( bytes );
if ( !ptr )
{
fprintf ( stderr, "malloc failed!\n" );
exit ( EXIT_FAILURE );
}
return ptr;
}
void* allocate_clear ( unsigned long bytes )
{
if ( bytes == 0 )
{
return NULL;
}
// malloc+memset we can do in one call using calloc.
void *ptr = calloc ( bytes, 1 );
if ( !ptr )
{
fprintf ( stderr, "calloc failed!\n" );
exit ( EXIT_FAILURE );
}
return ptr;
}
void* reallocate ( void *ptr, unsigned long bytes )
{
if ( bytes == 0 )
{
return NULL;
}
ptr = realloc ( ptr, bytes );
if ( !ptr )
{
fprintf ( stderr, "realloc failed!\n" );
exit ( EXIT_FAILURE );
}
return ptr;
}
static char **tokenize ( const char *input )
{
@ -171,7 +118,7 @@ static char **tokenize ( const char *input )
int num_tokens = 1;
//First entry is string that is modified.
retv = allocate ( 2 * sizeof ( char* ) );
retv = malloc ( 2 * sizeof ( char* ) );
retv[0] = strdup ( input );
retv[1] = NULL;
@ -182,7 +129,7 @@ static char **tokenize ( const char *input )
token != NULL;
token = strtok_r ( NULL, " ", &saveptr ) )
{
retv = reallocate ( retv, sizeof ( char* ) * ( num_tokens + 2 ) );
retv = realloc ( retv, sizeof ( char* ) * ( num_tokens + 2 ) );
retv[num_tokens + 1] = NULL;
retv[num_tokens] = token;
num_tokens++;
@ -383,18 +330,18 @@ winlist *cache_xattr;
winlist* winlist_new ()
{
winlist *l = allocate ( sizeof ( winlist ) );
winlist *l = malloc ( sizeof ( winlist ) );
l->len = 0;
l->array = allocate ( sizeof ( Window ) * ( WINLIST + 1 ) );
l->data = allocate ( sizeof ( void* ) * ( WINLIST + 1 ) );
l->array = malloc ( sizeof ( Window ) * ( WINLIST + 1 ) );
l->data = malloc ( sizeof ( void* ) * ( WINLIST + 1 ) );
return l;
}
int winlist_append ( winlist *l, Window w, void *d )
{
if ( l->len > 0 && !( l->len % WINLIST ) )
{
l->array = reallocate ( l->array, sizeof ( Window ) * ( l->len + WINLIST + 1 ) );
l->data = reallocate ( l->data, sizeof ( void* ) * ( l->len + WINLIST + 1 ) );
l->array = realloc ( l->array, sizeof ( Window ) * ( l->len + WINLIST + 1 ) );
l->data = realloc ( l->data, sizeof ( void* ) * ( l->len + WINLIST + 1 ) );
}
// Make clang-check happy.
// TODO: make clang-check clear this should never be 0.
@ -458,7 +405,7 @@ typedef struct
// allocate a pixel value for an X named color
// malloc a pixel value for an X named color
static unsigned int color_get ( const char *const name )
{
XColor color;
@ -515,7 +462,7 @@ XWindowAttributes* window_get_attributes ( Window w )
if ( idx < 0 )
{
XWindowAttributes *cattr = allocate ( sizeof ( XWindowAttributes ) );
XWindowAttributes *cattr = malloc ( sizeof ( XWindowAttributes ) );
if ( XGetWindowAttributes ( display, w, cattr ) )
{
@ -591,7 +538,7 @@ char* window_get_text_prop ( Window w, Atom atom )
{
if ( prop.encoding == XA_STRING )
{
res = allocate ( strlen ( ( char * ) prop.value ) + 1 );
res = malloc ( strlen ( ( char * ) prop.value ) + 1 );
// make clang-check happy.
if ( res )
{
@ -600,7 +547,7 @@ char* window_get_text_prop ( Window w, Atom atom )
}
else if ( Xutf8TextPropertyToTextList ( display, &prop, &list, &count ) >= Success && count > 0 && *list )
{
res = allocate ( strlen ( *list ) + 1 );
res = malloc ( strlen ( *list ) + 1 );
// make clang-check happy.
if ( res )
{
@ -753,7 +700,7 @@ client* window_client ( Window win )
return NULL;
}
client *c = allocate_clear ( sizeof ( client ) );
client *c = calloc ( 1, sizeof ( client ) );
c->window = win;
// copy xattr so we don't have to care when stuff is freed
memmove ( &c->xattr, attr, sizeof ( XWindowAttributes ) );
@ -1120,7 +1067,7 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
// filtered list display
textbox **boxes = allocate_clear ( sizeof ( textbox* ) * max_elements );
textbox **boxes = calloc ( 1, sizeof ( textbox* ) * max_elements );
for ( i = 0; i < max_elements; i++ )
{
@ -1161,8 +1108,8 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
}
// filtered list
char **filtered = allocate_clear ( sizeof ( char* ) * num_lines );
int *line_map = allocate_clear ( sizeof ( int ) * num_lines );
char **filtered = calloc ( num_lines, sizeof ( char* ) );
int *line_map = calloc ( num_lines, sizeof ( int ) );
unsigned int filtered_lines = 0;
if ( input && *input )
@ -1486,7 +1433,7 @@ MenuReturn menu ( char **lines, char **input, char *prompt, Time *time, int *shi
{
// Do not want to modify original string, so make copy.
// not eff..
char * str = allocate ( sizeof ( char ) * ( length_prefix + 1 ) );
char * str = malloc ( sizeof ( char ) * ( length_prefix + 1 ) );
memcpy ( str, filtered[0], length_prefix );
str[length_prefix] = '\0';
textbox_text ( text, str );
@ -1609,7 +1556,7 @@ SwitcherMode run_switcher_window ( char **input )
#ifdef HAVE_I3_IPC_H
}
#endif
char **list = allocate_clear ( sizeof ( char* ) * ( ids->len + 1 ) );
char **list = calloc ( ( ids->len + 1 ), sizeof ( char* ) );
int lines = 0;
// build the actual list
@ -1624,7 +1571,7 @@ SwitcherMode run_switcher_window ( char **input )
unsigned long wmdesktop;
char desktop[5];
desktop[0] = 0;
char *line = allocate ( strlen ( c->title ) + strlen ( c->class ) + classfield + 50 );
char *line = malloc ( strlen ( c->title ) + strlen ( c->class ) + classfield + 50 );
#ifdef HAVE_I3_IPC_H
if ( !config.i3_mode )
{

View file

@ -175,7 +175,7 @@ static char ** get_apps ( void )
continue;
}
retv = reallocate ( retv, ( index + 2 ) * sizeof ( char* ) );
retv = realloc ( retv, ( index + 2 ) * sizeof ( char* ) );
retv[index] = strdup ( dent->d_name );
retv[index + 1] = NULL;
index++;
@ -215,7 +215,7 @@ SwitcherMode run_switcher_dialog ( char **input )
if ( cmd_list == NULL )
{
cmd_list = allocate ( 2 * sizeof ( char * ) );
cmd_list = malloc ( 2 * sizeof ( char * ) );
cmd_list[0] = strdup ( "No applications found" );
cmd_list[1] = NULL;
}

View file

@ -53,7 +53,7 @@ static inline int execshssh ( const char *host )
/**
* I am not happy about this code, it causes 7 mallocs and frees
*/
char **args = allocate(sizeof(char*)*7);
char **args = malloc(sizeof(char*)*7);
int i=0;
args[i++] = config.terminal_emulator;
if(config.show_title) {
@ -202,7 +202,7 @@ static char ** get_ssh ( void )
continue;
}
retv = reallocate ( retv, ( index + 2 ) * sizeof ( char* ) );
retv = realloc ( retv, ( index + 2 ) * sizeof ( char* ) );
retv[index] = strndup ( &buffer[start], stop - start );
retv[index + 1] = NULL;
index++;
@ -240,7 +240,7 @@ SwitcherMode ssh_switcher_dialog ( char **input )
if ( cmd_list == NULL )
{
cmd_list = allocate ( 2 * sizeof ( char * ) );
cmd_list = malloc ( 2 * sizeof ( char * ) );
cmd_list[0] = strdup ( "No ssh hosts found" );
cmd_list[1] = NULL;
}

View file

@ -90,9 +90,9 @@ void parse_xresource_options ( Display *display )
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i )
{
char *name = ( char * ) allocate ( ( strlen ( namePrefix ) + 1 + strlen ( xrmOptions[i].name ) ) *
char *name = ( char * ) malloc ( ( strlen ( namePrefix ) + 1 + strlen ( xrmOptions[i].name ) ) *
sizeof ( char ) + 1 );
char *class = ( char * ) allocate ( ( strlen ( classPrefix ) + 1 + strlen ( xrmOptions[i].name ) ) *
char *class = ( char * ) malloc ( ( strlen ( classPrefix ) + 1 + strlen ( xrmOptions[i].name ) ) *
sizeof ( char ) + 1 );
sprintf ( name, "%s.%s", namePrefix, xrmOptions[i].name );
sprintf ( class, "%s.%s", classPrefix, xrmOptions[i].name );
@ -101,7 +101,7 @@ void parse_xresource_options ( Display *display )
{
if ( xrmOptions[i].type == xrm_String )
{
*xrmOptions[i].str = ( char * ) allocate ( xrmValue.size * sizeof ( char ) );
*xrmOptions[i].str = ( char * ) malloc ( xrmValue.size * sizeof ( char ) );
strncpy ( *xrmOptions[i].str, xrmValue.addr, xrmValue.size );
}
else if ( xrmOptions[i].type == xrm_Number )