Skip to content

Instantly share code, notes, and snippets.

@o11c o11c/gist:7729355
Created Dec 1, 2013

Embed
What would you like to do?
// 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
You can’t perform that action at this time.