Skip to content

Instantly share code, notes, and snippets.

@m4rw3r
Created March 29, 2012 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m4rw3r/2238057 to your computer and use it in GitHub Desktop.
Save m4rw3r/2238057 to your computer and use it in GitHub Desktop.
#ifndef CARRAYMACROS_H
#define CARRAYMACROS_H 1
#include <inttypes.h>
#include <stdio.h>
#define ARRAY_SIZE(array) array ## _num
#define ARRAY_MAX(array) array ## _max
#define ARRAY_CDECL(array, type) \
uint32_t ARRAY_MAX(array); \
uint32_t ARRAY_SIZE(array); \
type *array;
#define ARRAY_INIT(array, type, initial_size) \
do { array = malloc(sizeof(type) * initial_size); \
ARRAY_MAX(array) = initial_size; \
ARRAY_SIZE(array) = 0; } while(0)
/* Resizes array of type type to at least ARRAY_SIZE(array) */
#define ARRAY_MAKESIZE(array, type) \
while (ARRAY_MAX(array) < ARRAY_SIZE(frame->variables)) { ARRAY_MAX(array) *= 2; \
void *tmp = realloc(array, (sizeof(type) * ARRAY_MAX(array))); \
if( ! tmp) { \
/* TODO: Move error code to separate file and remove \
include of stdio.h */ \
fprintf(stderr, "ERROR: Couldn't realloc() Frame variables."); \
exit(-1); \
} array = tmp; }
#define ARRAY_PACK(dest, source, type) \
do { void *tmp = realloc(dest, (sizeof(type) * ARRAY_MAX(source))); \
if( ! tmp) { \
/* TODO: Move error code to separate file and remove \
include of stdio.h */ \
fprintf(stderr, "ERROR: Couldn't realloc() Frame variables."); \
exit(-1); \
} dest = tmp; }
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment