Skip to content

Instantly share code, notes, and snippets.

@o11c
Created December 1, 2013 06:55
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 o11c/7729355 to your computer and use it in GitHub Desktop.
Save o11c/7729355 to your computer and use it in GitHub Desktop.
// amd64
typedef unsigned long size_t;
void *tor_realloc(void *, size_t);
typedef struct smartlist_t {
/** @{ */
/** <b>list</b> has enough capacity to store exactly <b>capacity</b> elements
* before it needs to be resized. Only the first <b>num_used</b> (\<=
* capacity) elements point to valid data.
*/
void **list;
int num_used;
int capacity;
/** @} */
} smartlist_t;
void
smartlist_ensure_capacity(smartlist_t *sl, int size)
{
#define MAX_CAPACITY (0x7fffffff)
if (size > sl->capacity) {
int higher = sl->capacity;
if ((size > MAX_CAPACITY/2)) {
higher = MAX_CAPACITY;
} else {
while (size > higher)
higher *= 2;
}
sl->capacity = higher;
sl->list = tor_realloc(sl->list, sizeof(void*)*((size_t)sl->capacity));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment