static int event_reserve_socket_set(SocketSet **socket_set, int reserve) { SocketSet *bytes; if (*socket_set != NULL && (*socket_set)->allocated >= reserve) { return 0; } reserve = GROW_ALLOCATION(reserve); if (*socket_set != NULL) { bytes = realloc(*socket_set, sizeof(SocketSet) + sizeof(SOCKET) * reserve); } else { bytes = calloc(1, sizeof(SocketSet) + sizeof(SOCKET) * reserve); } if (bytes == NULL) { errno = ENOMEM; return -1; } *socket_set = bytes; (*socket_set)->allocated = reserve; return 0; }
// sets errno on error int array_create(Array *array, int reserved, int size, int relocatable) { reserved = GROW_ALLOCATION(reserved); array->allocated = 0; array->count = 0; array->size = size; array->relocatable = relocatable; array->bytes = calloc(reserved, relocatable ? size : (int)sizeof(void *)); if (array->bytes == NULL) { errno = ENOMEM; return -1; } array->allocated = reserved; return 0; }
// sets errno on error int array_reserve(Array *array, int count) { int size = array->relocatable ? array->size : (int)sizeof(void *); uint8_t *bytes; if (array->allocated >= count) { return 0; } count = GROW_ALLOCATION(count); bytes = realloc(array->bytes, count * size); if (bytes == NULL) { errno = ENOMEM; return -1; } array->allocated = count; array->bytes = bytes; return 0; }