void TestLinkedListReduce(CuTest* tc) { LinkedList* list = util_linkedList(); Memory* data; for (int i = 0; i < 5; i++) { data = util_alloc(sizeof(int), &dummyDealloc); *data = i; util_list_add_last(list, data); } Memory* acc = util_alloc(sizeof(int), &dummyDealloc); *acc = 0; util_list_reduce(list, &addIntFunction, acc); /* 0 + 0 + 1 + 2 + 3 + 4 = 10 */ CuAssertTrue(tc, 10 == *acc); util_release((Memory*)list); util_release(acc); }
void TestLinkedListMap(CuTest* tc) { LinkedList* list = util_linkedList(); Memory* data; for (int i = 0; i < 5; i++) { data = util_alloc(sizeof(int), &dummyDealloc); *data = i; util_list_add_last(list, data); } util_list_map(list, &doubleIntFunction); data = util_list_first(list); CuAssertTrue(tc, 0 == *data); data = util_list_last(list); CuAssertTrue(tc, 8 == *data); util_release((Memory*)list); }
compState_t *tsCompStateAddFrame(compState_t *state) { compState_t *tmp, *walk, *last, *parent; if(state->type != eCompAnimation) { return state; } if(state->parent == NULL) { parent = state; } else { parent = state->parent; } tmp = util_alloc(sizeof(compState_t), NULL); tsCompStateInit(tmp, state->texture, state->type); tmp->parent = parent; last = parent; for(walk = parent; walk != NULL; walk = walk->next) { last = walk; } last->next = tmp; tmp->frame = last->frame + 1; parent->total = tmp->frame + 1; return tmp; }
void TestLinkedListRemoveLast(CuTest* tc) { uint32_t initialAllocCount = util_total_alloc_count(); LinkedList* list = util_linkedList(); Memory* data; for (int i = 0; i < 5; i++) { data = util_alloc(sizeof(int), &dummyDealloc); *data = i; util_list_add_first(list, data); } CuAssertTrue(tc, util_list_count(list) == 5); for (int i = 0; i < 5; i++) { CuAssertTrue(tc, util_list_count(list) == 5 - i); data = util_list_remove_last(list); CuAssertTrue(tc, *data == i); } util_release((Memory*)list); util_empty_autoreleasepool(); printf("log: %d %d", initialAllocCount, util_total_alloc_count()); CuAssertTrue(tc, initialAllocCount == util_total_alloc_count()); }
void LinkedListGetRest(CuTest* tc) { uint32_t initialAllocCount = util_total_alloc_count(); LinkedList* list = util_linkedList(); Memory* data; for (int i = 0; i < 5; i++) { data = util_alloc(sizeof(int), &dummyDealloc); *data = i; util_list_add_last(list, data); } LinkedList* rest = util_list_rest(list); Memory* first = util_list_first(rest); Memory* last = util_list_last(rest); CuAssertTrue(tc, 1 == *first); CuAssertTrue(tc, 4 == *last); util_release((Memory*)rest); util_release((Memory*)list); CuAssertTrue(tc, initialAllocCount == util_total_alloc_count()); }
void TestLinkedListAddAtIndex(CuTest* tc) { uint32_t initialAllocCount = util_total_alloc_count(); LinkedList* list = util_linkedList(); Memory* data; for (int i = 1; i < 5; i++) { data = util_alloc(sizeof(int), &dummyDealloc); *data = i; util_list_add_last(list, data); } data = util_alloc(sizeof(int), &dummyDealloc); *data = 42; util_list_add_at_index(list, data, 2); CuAssertTrue(tc, util_list_count(list) == 5); data = util_list_index(list, 2); CuAssertTrue(tc, 42 == *data); data = util_list_index(list, 3); CuAssertTrue(tc, 3 == *data); util_release((Memory*)list); CuAssertTrue(tc, initialAllocCount == util_total_alloc_count()); }
int aio_socket_sendto_v(aio_socket_t socket, const struct sockaddr *addr, socklen_t addrlen, socket_bufvec_t* vec, int n, aio_onsend proc, void* param) { struct aio_context *ctx = (struct aio_context*)socket; struct aio_context_action *aio; aio = util_alloc(ctx); aio->action = iocp_send; aio->send.proc = proc; aio->send.param = param; assert(0 == (AIO_WRITE & InterlockedOr(&ctx->flags, AIO_WRITE))); if(SOCKET_ERROR == WSASendTo(ctx->socket, vec, (DWORD)n, NULL/*&dwBytes*/, 0, addr, addrlen, &aio->overlapped, NULL)) { return aio_socket_result(aio, AIO_WRITE); } return 0; }
int aio_socket_connect(aio_socket_t socket, const struct sockaddr *addr, socklen_t addrlen, aio_onconnect proc, void* param) { struct aio_context *ctx = (struct aio_context*)socket; struct aio_context_action *aio; aio = util_alloc(ctx); aio->action = iocp_connect; aio->connect.proc = proc; aio->connect.param = param; assert(0 == (AIO_WRITE & InterlockedOr(&ctx->flags, AIO_WRITE))); if (!ConnectEx(ctx->socket, addr, addrlen, NULL, 0, NULL, &aio->overlapped)) { return aio_socket_result(aio, AIO_WRITE); } return 0; }
int aio_socket_recv_v(aio_socket_t socket, socket_bufvec_t* vec, int n, aio_onrecv proc, void* param) { DWORD flags = 0; struct aio_context *ctx = (struct aio_context*)socket; struct aio_context_action *aio; aio = util_alloc(ctx); aio->action = iocp_recv; aio->recv.proc = proc; aio->recv.param = param; assert(0 == (AIO_READ & InterlockedOr(&ctx->flags, AIO_READ))); if(SOCKET_ERROR == WSARecv(ctx->socket, vec, n, NULL/*&dwBytes*/, &flags, &aio->overlapped, NULL)) { return aio_socket_result(aio, AIO_READ); } return 0; }
void TestLinkedListAddLast(CuTest* tc) { uint32_t initialAllocCount = util_total_alloc_count(); LinkedList* list = util_linkedList(); for (int i = 1; i < 5; i++) { util_list_add_last(list, util_alloc(200, &dummyDealloc)); CuAssertTrue(tc, !util_list_is_empty(list)); CuAssertTrue(tc, util_list_count(list) == i); } util_release((Memory*)list); CuAssertTrue(tc, initialAllocCount == util_total_alloc_count()); }
int aio_socket_sendto_v(aio_socket_t socket, const struct sockaddr *addr, socklen_t addrlen, socket_bufvec_t* vec, int n, aio_onsend proc, void* param) { struct aio_context *ctx = (struct aio_context*)socket; struct aio_context_action *aio; aio = util_alloc(ctx); aio->action = iocp_send; aio->send.proc = proc; aio->send.param = param; if(SOCKET_ERROR == WSASendTo(ctx->socket, vec, (DWORD)n, NULL, 0, addr, addrlen, &aio->overlapped, NULL)) { DWORD ret = WSAGetLastError(); if(WSA_IO_PENDING != ret) { util_free(aio); return ret; } } return 0; }
int aio_socket_connect(aio_socket_t socket, const struct sockaddr *addr, socklen_t addrlen, aio_onconnect proc, void* param) { struct aio_context *ctx = (struct aio_context*)socket; struct aio_context_action *aio; aio = util_alloc(ctx); aio->action = iocp_connect; aio->connect.proc = proc; aio->connect.param = param; if(!ConnectEx(ctx->socket, addr, addrlen, NULL, 0, NULL, &aio->overlapped)) { DWORD ret = WSAGetLastError(); if(ERROR_IO_PENDING != ret) { util_free(aio); return ret; } } return 0; }
int aio_socket_send_v(aio_socket_t socket, socket_bufvec_t* vec, int n, aio_onsend proc, void* param) { DWORD dwBytes = 0; struct aio_context *ctx = (struct aio_context*)socket; struct aio_context_action *aio; aio = util_alloc(ctx); aio->action = iocp_send; aio->send.proc = proc; aio->send.param = param; if(SOCKET_ERROR == WSASend(ctx->socket, vec, n, &dwBytes, 0, &aio->overlapped, NULL)) { DWORD ret = WSAGetLastError(); if(WSA_IO_PENDING != ret) { util_free(aio); return ret; } } return 0; }
int aio_socket_accept(aio_socket_t socket, aio_onaccept proc, void* param) { int ret; DWORD dwBytes = 0; WSAPROTOCOL_INFOW pi; struct aio_context *ctx = (struct aio_context*)socket; struct aio_context_action *aio; ret = sizeof(pi); if (0 != getsockopt(ctx->socket, SOL_SOCKET, SO_PROTOCOL_INFO, (char*)&pi, &ret)) return WSAGetLastError(); aio = util_alloc(ctx); aio->action = iocp_accept; aio->accept.proc = proc; aio->accept.param = param; aio->accept.socket = WSASocket(pi.iAddressFamily, SOCK_STREAM, IPPROTO_TCP, NULL, 0, WSA_FLAG_OVERLAPPED); if(INVALID_SOCKET == aio->accept.socket) { closesocket(aio->accept.socket); util_free(aio); return WSAGetLastError(); } dwBytes = sizeof(aio->accept.buffer) / 2; assert(0 == (AIO_READ & InterlockedOr(&ctx->flags, AIO_READ))); if (!AcceptEx(ctx->socket, aio->accept.socket, aio->accept.buffer, 0, dwBytes, dwBytes, &dwBytes, &aio->overlapped)) { ret = aio_socket_result(aio, AIO_READ); if(0 != ret) { closesocket(aio->accept.socket); return ret; } } return 0; }
void TestLinkedListGetEqual(CuTest* tc) { uint32_t initialAllocCount = util_total_alloc_count(); LinkedList* list = util_linkedList(); Memory* data; for (int i = 1; i < 5; i++) { data = util_alloc(sizeof(int), &dummyDealloc); *data = i; util_list_add_last(list, data); } data = util_list_equal(list, data, &comparePointer); CuAssertTrue(tc, 4 == *data); util_release((Memory*)list); CuAssertTrue(tc, initialAllocCount == util_total_alloc_count()); }
int aio_socket_recvfrom_v(aio_socket_t socket, socket_bufvec_t* vec, int n, aio_onrecvfrom proc, void* param) { DWORD flags = 0; DWORD dwBytes = 0; struct aio_context *ctx = (struct aio_context*)socket; struct aio_context_action *aio; aio = util_alloc(ctx); aio->action = iocp_recvfrom; aio->recvfrom.proc = proc; aio->recvfrom.param = param; aio->recvfrom.addrlen = sizeof(aio->recvfrom.addr); if(SOCKET_ERROR == WSARecvFrom(ctx->socket, vec, (DWORD)n, &dwBytes, &flags, (struct sockaddr *)&aio->recvfrom.addr, &aio->recvfrom.addrlen, &aio->overlapped, NULL)) { DWORD ret = WSAGetLastError(); if(WSA_IO_PENDING != ret) { util_free(aio); return ret; } } return 0; }
int module_load(const char *modules_path) { DIR *dir = opendir(modules_path); struct dirent *ent = NULL; void *module_handle = NULL; void (*module_init)() = NULL; if (dir == NULL) { log_error("%s","Error loading modules!\n"); return -1; } char file_path[256]; while ((ent = readdir(dir)) != NULL) { module_t *module = NULL; if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue; sprintf(file_path, "%s%s/%s%s.so", modules_path, ent->d_name, MODULES_PREFIX, ent->d_name); if (!fs_file_exists(file_path)) { log_error("%s%s%s","Error loading mod_", ent->d_name, ".so. No such file!\n"); continue; } module_handle = dlopen(file_path, RTLD_LAZY); if (module_handle == NULL) { log_error("%s%s%s","Error loading mod_", ent->d_name, ".so. Invalid module!\n"); continue; } module_init = dlsym(module_handle, "init"); if (module_init == NULL) { log_error("%s%s%s","Module mod_", ent->d_name, ".so doesn`t have a init function. Ignoring...!\n"); dlclose(module_handle); continue; } log_message("%s%s\n","Loading module: mod_", ent->d_name); num_modules++; modules = util_alloc(modules, num_modules * sizeof(*module)); if (modules != NULL) { module_init(); module = malloc(sizeof(*module)); if (module != NULL) { module->name = malloc(strlen(ent->d_name) + strlen(MODULES_PREFIX) + 1); sprintf(module->name, "%s%s", MODULES_PREFIX, ent->d_name); module->handle = module_handle; modules[num_modules-1] = module; continue; } } log_message("%s%s\n", "Error allocating memory for module mod_", ent->d_name); dlclose(module_handle); num_modules--; return -1; } closedir(dir); return 0; }
LinkedList* util_linkedList() { return (LinkedList*)util_alloc(sizeof(LinkedList), &deallocList); }
static ListNode* nodeWithData(Memory *data) { ListNode* node = (ListNode*)util_alloc(sizeof(ListNode), &deallocNode); node->data = data; return node; }