static void tls_specific_dtor(void *value) { int kvindex; mapkey_t keys[32]; void *values[32]; tls_base_t *base; if (!value) return; base = (tls_base_t *)value; while ((kvindex = map_get_values(&base->kvmap, keys, values, DTOR_KV_CAPACITY))) { while (kvindex--) { tls_entry_t *entry = (tls_entry_t *)(values[kvindex]); map_remove(&base->kvmap, keys[kvindex]); if (entry->dtor != NULL) entry->dtor(keys[kvindex], entry->value); com_free(g_tls_allocator, entry); } } map_destroy(&base->kvmap); com_free(g_tls_allocator, base); }
sock_info *dcc_init_connect(sock_info *sock, unsigned long ip, int port) { struct sockaddr_in dest; dest.sin_family = AF_INET; dest.sin_port = htons(port); dest.sin_addr.s_addr = ip; memset(&(dest.sin_zero), 0, 8); if ((sock->sockfd = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) { com_free(sock); return NULL; } if (connect(sock->sockfd, (struct sockaddr *)&dest, sizeof(struct sockaddr))) { com_free(sock); return NULL; } return sock; }
void dcc_free(dbase_nicks *nick) { int i; for (i = 0; i < com_sock_array_count; i++) { if ((com_sock_array[i]->type == SOCK_DCC) && (com_sock_array[i]->from == nick)) { com_free(com_sock_array[i]); i--; } } }
bool array_copy(const array_t *src, array_t *dst) { if (src == NULL || dst == NULL) { s_fatal_error(1, "Cannot copy NULL array."); return false; } array_t copy = *dst; const size_t src_buffer_size = (src->size * src->obj_size); const size_t dst_buffer_size = (copy.capacity * copy.obj_size); if (copy.buf) { memset(copy.buf, 0, dst_buffer_size); if (dst_buffer_size < src_buffer_size) { // release the buffer since it can't hold the source buffer dst->buf = NULL; com_free(copy.allocator, copy.buf); copy.buf = NULL; copy.capacity = 0; } else { // reuse the buffer copy.capacity = dst_buffer_size / src->obj_size; } } copy.size = 0; copy.obj_size = src->obj_size; if (!array_resize(©, src->size)) { s_fatal_error(1, "Failed to resize destination array."); return false; } else if (!copy.buf) { s_fatal_error(1, "Destination array buffer NULL."); return false; } if (src->buf) { if (!array_resize(©, src->size)) { s_fatal_error(1, "Failed to copy array."); return false; } if (src->size > 0 && copy.size == src->size) memcpy(copy.buf, src->buf, src_buffer_size); } *dst = copy; return true; }
void array_destroy(array_t *self) { if (self == NULL) { s_fatal_error(1, "Cannot destroy NULL array."); return; } allocator_t *alloc = self->allocator; if (self->buf != NULL) com_free(alloc, self->buf); memset(self, 0, sizeof(*self)); }
int buffer_destroy(buffer_t *buffer) { if (!buffer) { errno = EBADF; s_log_error("Attempt to destroy a NULL buffer."); return -1; } if (buffer->ptr && ! buffer->outside) com_free(buffer->alloc, buffer->ptr); memset(buffer, 0, sizeof(*buffer)); return 0; }
static void mapnode_destroy_r(map_t *map, mapnode_t *node) { allocator_t *alloc = map->allocator; mapnode_t *l, *r; if (node == NIL) return; l = node->left; r = node->right; map->ops.destroy_key(node->key, alloc); map->ops.destroy_value(node->p, alloc); com_free(map->allocator, node); mapnode_destroy_r(map, l); mapnode_destroy_r(map, r); }
bool array_reserve(array_t *self, size_t capacity) { size_t new_size, new_cap, orig_size; char *new_buf = NULL; bool tried_min = false; if (capacity <= self->capacity || capacity == 0) return true; new_cap = self->capacity * 2; if (new_cap < capacity) { new_cap = capacity; /* minimum requested is larger, use it */ tried_min = true; } orig_size = self->size * self->obj_size; reserve_capacity: new_size = new_cap * self->obj_size; new_buf = (char *)com_malloc(self->allocator, new_size); if (NULL == new_buf) { /* in the event that the new buffer can't be allocated, try one more route before giving up */ if (!tried_min) { /* if the minimum capacity requested hasn't been tried yet, try it */ tried_min = true; new_cap = capacity; goto reserve_capacity; } s_fatal_error(1, "Failed to reserve %zu elements for array.", new_cap); return false; } if (self->buf && orig_size) memcpy(new_buf, self->buf, orig_size); memset(new_buf + orig_size, 0, new_size - orig_size); if (self->buf) com_free(self->allocator, self->buf); self->buf = new_buf; self->capacity = new_cap; return true; }
void *dcc_init(void *arg) { dcc_init_arg *args = (dcc_init_arg*)arg; sock_info *sock; dbase_nicks *from; from = nicks_getinfo(args->num, NULL, -1); if (!from) { xfree(args->num); xfree(args); return 0; } if (!(sock = com_sock_create(SOCK_DCC))) { xfree(args->num); xfree(args); return 0; } if (!dcc_init_connect(sock, args->ip, args->port)) { com_free(sock); xfree(args->num); xfree(args); return 0; } sock->from = from; com_message(sock, conf->os->numeric, from->numeric, MODE_NOTICE, "Welcome to %s DCC interface.", conf->host); com_message(sock, conf->os->numeric, from->numeric, MODE_NOTICE, "Compiled %s on %s", build_date, os_name); dcc_console_text('c', "[%s] connected to the DCC interface...", sock->from->nickserv->nick); xfree(args->num); xfree(args); return 0; }
static int file_close(stream_t *stream) { PHYSFS_File *file; int r = 0; if (file_check_context(stream)) r = -1; file = stream->context.pfs.file; if (file && ! PHYSFS_close(file)) { s_log_error("Error closing file: %s. (File: %s)", pfs_get_error(), stream->context.pfs.file_path); stream->error = STREAM_ERROR_FAILURE; r = -1; } if (stream->context.pfs.file_path) com_free(stream->alloc, stream->context.pfs.file_path); return r; }
void wad_free(void) { com_free(lk_wad); }
static void mapnode_remove(map_t *map, mapnode_t *node) { allocator_t *alloc = map->allocator; mapnode_t *destroyed, *y, *z; if (node->left == NIL) { destroyed = node; y = node->right; } else if (node->right == NIL) { destroyed = node; y = node->left; } else { destroyed = node->left; while (destroyed->right != NIL) destroyed = destroyed->right; y = destroyed->left; node->key = destroyed->key; node->p = destroyed->p; } z = destroyed->parent; if (y != NIL) y->parent = z; if (z == NIL) { map->root = y; goto finish_removal; } if (destroyed == z->left) { z->left = y; } else { z->right = y; } if (IS_BLACK(destroyed)) { while (y != map->root && IS_BLACK(y)) { mapnode_t *sibling; int dir = !(y == z->left); sibling = OPP_NODE(z, dir); if (IS_RED(sibling)) { sibling->color = BLACK; z->color = RED; g_rotations[dir](map, z); sibling = OPP_NODE(z, dir); } if (IS_BLACK(sibling->left) && IS_BLACK(sibling->right)) { sibling->color = RED; y = z; z = z->parent; } else { if (IS_BLACK(OPP_NODE(sibling, dir))) { DIR_NODE(sibling, dir)->color = BLACK; sibling->color = RED; g_rotations[!dir](map, sibling); sibling = OPP_NODE(z, dir); } sibling->color = z->color; z->color = BLACK; OPP_NODE(sibling, dir)->color = BLACK; g_rotations[dir](map, z); y = map->root; } } y->color = BLACK; } finish_removal: com_free(alloc, destroyed); map->size -= 1; #if !defined(NDEBUG) map_test(map->root); #endif }