static void spawn_free(void *vproc) { if (vproc) { spawn_proc *proc = vproc; spawn_proc *list = spawn_list; char *argv0 = proc->argv0; p_spawn_t *pp = proc->proc; proc->argv0 = 0; proc->proc = 0; p_free(argv0); if (pp) { p_send(pp, (char *)0, -9); p_spawf(pp, 0); } if (list == proc) { spawn_list = proc->next; } else while (list && list->next) { if (list->next == proc) { list->next = proc->next; break; } list = list->next; } p_free(proc); } }
psckt_t * psckt_connect(const char *addr, int port, void *ctx, psckt_cb_t *callback) { int sfd; psckt_t *sock = p_malloc(sizeof(psckt_t)); struct addrinfo *ai, *ailist = sock? psckt_get_ailist(addr, port, 0) : 0; if (!ailist) { if (sock) p_free(sock); return 0; } for (ai=ailist ; ai ; ai=ai->ai_next) { sfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); if (sfd == -1) continue; if (connect(sfd, ai->ai_addr, ai->ai_addrlen) != -1) break; close(sfd); } if (!ai) { p_free(sock); sock = 0; } freeaddrinfo(ailist); if (!sock) return 0; sock->fd = sfd; sock->peer = p_strcpy(addr); return psckt_setup(sock, ctx, callback); }
psckt_t * psckt_accept(psckt_t *listener, char **ppeer, void *ctx, psckt_cb_t *callback) { struct sockaddr_storage sad; socklen_t lsad = sizeof(struct sockaddr_storage); struct sockaddr *psad = (struct sockaddr *)&sad; char host[NI_MAXHOST]; psckt_t *sock = p_malloc(sizeof(psckt_t)); int sfd, lfd = sock->fd; *ppeer = 0; if (listener->peer || !sock) { /* listeners have no peer */ if (sock) p_free(sock); return 0; } sfd = accept(listener->fd, psad, &lsad); if (sfd==-1 || getnameinfo(psad, lsad, host, NI_MAXHOST, 0, 0, 0)) { close(sfd); p_free(sock); return 0; } sock->fd = sfd; sock->peer = *ppeer = p_strcpy(host); return psckt_setup(sock, ctx, callback); }
static pboolean compare_socket_addresses (const PSocketAddress *addr1, const PSocketAddress *addr2) { if (addr1 == NULL || addr2 == NULL) return FALSE; pchar *addr_str1 = p_socket_address_get_address (addr1); pchar *addr_str2 = p_socket_address_get_address (addr2); if (addr_str1 == NULL || addr_str2 == NULL) { p_free (addr_str1); p_free (addr_str2); return FALSE; } pboolean addr_cmp = (strcmp (addr_str1, addr_str2) == 0 ? TRUE : FALSE); p_free (addr_str1); p_free (addr_str2); if (addr_cmp == FALSE) return FALSE; if (p_socket_address_get_family (addr1) != p_socket_address_get_family (addr2)) return FALSE; if (p_socket_address_get_native_size (addr1) != p_socket_address_get_native_size (addr2)) return FALSE; return TRUE; }
ufs_inode_t* ufs_traverse_path(ufs_t *mount, const char *_path) { uint32_t i; uint32_t inum = 2; // 2 == root ufs_inode_t *inode = p_alloc(mount->pool, sizeof(ufs_inode_t)); char *path = p_alloc(mount->pool, strlen(_path)+1); strcpy(path, _path); if (!ufs_load_inode(mount, inode, inum)) goto fail; char *last, *elem; for (elem = strtok_r(path, "/", &last); elem; elem = strtok_r(NULL, "/", &last)) { uint32_t next_inum = 0; uint8_t *dir = ufs_read_inode_data(mount, inode); if (!dir) goto fail; for (i=0; inode->size; ) { ufs_dir_t *entry = (ufs_dir_t*)&dir[i]; fix_endian(entry->inum); fix_endian(entry->len); fix_endian(entry->namelen); if (entry->inum == 0) break; if ((entry->namelen == strlen(elem)) && (strncmp(elem, entry->name, entry->namelen) == 0)) { next_inum = entry->inum; break; } i += entry->len; } p_free(dir); if (next_inum == 0) { sprintf(mount->error_str, "'%s' in '%s' doesn't exist", elem, _path); goto fail; } inum = next_inum; if (!ufs_load_inode(mount, inode, inum)) goto fail; } p_free(path); return inode; fail: p_free(inode); p_free(path); return NULL; }
int GaFreeScratch(void) { if (nScratchP>0) { p_free(gaxScratch); p_free(gayScratch); } if (nScratchS>0) p_free(gasScratch); if (nScratch>0) { p_free(xScratch); p_free(yScratch); } nScratchP= nScratchS= nScratch= 0; return 0; }
int p_dclose(p_dir *dir) { int flag = closedir(dir->dir); p_free(dir->dirname); p_free(dir); return flag; }
P_LIB_API PDir * p_dir_new (const pchar *path, PError **error) { PDir *ret; pchar *pathp; if (P_UNLIKELY (path == NULL)) { p_error_set_error_p (error, (pint) P_ERROR_IO_INVALID_ARGUMENT, 0, "Invalid input argument"); return NULL; } if (P_UNLIKELY ((ret = p_malloc0 (sizeof (PDir))) == NULL)) { p_error_set_error_p (error, (pint) P_ERROR_IO_NO_RESOURCES, 0, "Failed to allocate memory for directory structure"); return NULL; } if (P_UNLIKELY (!GetFullPathNameA (path, MAX_PATH, ret->path, NULL))) { p_error_set_error_p (error, (pint) p_error_get_last_io (), p_error_get_last_system (), "Failed to call GetFullPathNameA() to get directory path"); p_free (ret); return NULL; } /* Append the search pattern "\\*\0" to the directory name */ pathp = strchr (ret->path, '\0'); if (ret->path < pathp && *(pathp - 1) != '\\' && *(pathp - 1) != ':') *pathp++ = '\\'; *pathp++ = '*'; *pathp = '\0'; /* Open directory stream and retrieve the first entry */ ret->search_handle = FindFirstFileA (ret->path, &ret->find_data); if (P_UNLIKELY (ret->search_handle == INVALID_HANDLE_VALUE)) { p_error_set_error_p (error, (pint) p_error_get_last_io (), p_error_get_last_system (), "Failed to call FindFirstFileA() to open directory stream"); p_free (ret); return NULL; } ret->cached = TRUE; ret->orig_path = p_strdup (path); return ret; }
void buffer_free(buffer_t **_buf) { struct real_buffer *buf = (struct real_buffer *)*_buf; *_buf = NULL; if (buf->alloced) p_free(buf->pool, buf->w_buffer); if (buf->pool != NULL) p_free(buf->pool, buf); }
static void ClearPrefixes(void) { int i, n= nYpPrefixes; char **prefixes= ypPrefixes; nYpPrefixes= 0; for (i=0 ; i<n ; i++) p_free(prefixes[i]); maxYpPrefixes= 0; ypPrefixes= 0; p_free(prefixes); }
P_LIB_API void p_error_free (PError *error) { if (P_UNLIKELY (error == NULL)) return; if (error->message != NULL) p_free (error->message); p_free (error); }
static uint8_t ufs_load_inode(ufs_t *mount, ufs_inode_t *inode, uint32_t inum) { assert(sizeof(ufs_inode_t) == 128); /* Which cylinder group is this inode in? */ const uint32_t group_num = inum / mount->superblock.ipg; /* Index of this inode in its cylinder group's inode table */ const uint32_t group_ino_offset = inum % mount->superblock.ipg; /* Fragment address that contains inode */ const uint32_t frag_addr = ufs_group_base(mount, group_num) + mount->superblock.iblkno + ((group_ino_offset * 128) / mount->frag_size); /* Byte offset into the fragment where the inode begins */ const uint32_t frag_offset = (group_ino_offset * 128) % mount->frag_size; uint32_t i; uint8_t *buf = p_alloc(mount->pool, mount->frag_size); // slog("group_num = %u, ino_offset=%u, addr = 0x%08x, offset = 0x%08x\n", group_num, group_ino_offset, frag_addr, frag_offset); // slog("mount->superblock.iblkno = 0x%08x\n", mount->superblock.iblkno); if (!ufs_read_frag(mount, buf, frag_addr)) goto fail; memcpy(inode, buf + frag_offset, 128); fix_endian(inode->mode); fix_endian(inode->nlink); fix_endian(inode->uid); fix_endian(inode->gid); fix_endian(inode->size_hi); fix_endian(inode->size); fix_endian(inode->atime); fix_endian(inode->mtime); fix_endian(inode->ctime); for (i=0; i<12; i++) fix_endian(inode->direct[i]); for (i=0; i<3; i++) fix_endian(inode->indirect[i]); fix_endian(inode->flags); fix_endian(inode->blocks); fix_endian(inode->gen); p_free(buf); return 1; fail: if (buf) p_free(buf); return 0; }
static void dcrypt_gnutls_ctx_sym_set_key_iv_random(struct dcrypt_context_symmetric *ctx) { if(ctx->key.data != NULL) p_free(ctx->pool, ctx->key.data); if(ctx->iv.data != NULL) p_free(ctx->pool, ctx->iv.data); ctx->key.data = p_malloc(ctx->pool, gnutls_cipher_get_key_size(ctx->cipher)); random_fill(ctx->key.data, gnutls_cipher_get_key_size(ctx->cipher)); ctx->key.size = gnutls_cipher_get_key_size(ctx->cipher); ctx->iv.data = p_malloc(ctx->pool, gnutls_cipher_get_iv_size(ctx->cipher)); random_fill(ctx->iv.data, gnutls_cipher_get_iv_size(ctx->cipher)); ctx->iv.size = gnutls_cipher_get_iv_size(ctx->cipher); }
static uint8_t* svfs_read_inode_data(svfs_t *mount, svfs_inode_t *inode) { uint8_t *tmp = p_alloc(mount->pool, mount->blocksize); uint8_t *buf = p_alloc(mount->pool, inode->size); uint32_t i, len = 0; // The first 10 block pointers in the inode point to data // The addr[10] is a L1 block pointer, [11] is L2, and [12] is L3 for (i=0; (len < inode->size) && (i < 10); i++) { uint32_t chunk_size = inode->size - len; if (chunk_size > mount->blocksize) chunk_size = mount->blocksize; if (!svfs_read_block(mount, tmp, inode->addr[i])) { sprintf(mount->error_str, "couldn't read svfs block num %u at L0", inode->addr[i]); goto fail; } memcpy(buf + len, tmp, chunk_size); len += chunk_size; } if (!svfs_read_block(mount, tmp, inode->addr[10])) { sprintf(mount->error_str, "couldn't read svfs L1 block ptr %u", inode->addr[10]); goto fail; } else if (!svfs_read_level(mount, inode, buf, &len, (uint32_t*)tmp, 1)) goto fail; if (!svfs_read_block(mount, tmp, inode->addr[11])) { sprintf(mount->error_str, "couldn't read svfs L2 block ptr %u", inode->addr[11]); goto fail; } else if (!svfs_read_level(mount, inode, buf, &len, (uint32_t*)tmp, 2)) goto fail; if (!svfs_read_block(mount, tmp, inode->addr[12])) { sprintf(mount->error_str, "couldn't read svfs L3 block ptr %u", inode->addr[12]); goto fail; } else if (!svfs_read_level(mount, inode, buf, &len, (uint32_t*)tmp, 3)) goto fail; p_free(tmp); return buf; fail: p_free(tmp); p_free(buf); return NULL; }
P_LIB_API void p_shm_free (PShm *shm) { if (P_UNLIKELY (shm == NULL)) return; pp_shm_clean_handle (shm); if (P_LIKELY (shm->platform_key != NULL)) p_free (shm->platform_key); p_free (shm); }
static void ClearList(void) { void *item, **list= memlist; if (list) { while (nlist>0) { item= list[--nlist]; list[nlist]= 0; if (item) p_free(item); } memlist= 0; p_free(list); } keeplist= 0; }
void hash_table_destroy(struct hash_table **_table) { struct hash_table *table = *_table; *_table = NULL; if (!table->node_pool->alloconly_pool) { hash_table_destroy_nodes(table); destroy_node_list(table, table->free_nodes); } p_free(table->table_pool, table->nodes); p_free(table->table_pool, table); }
P_LIB_API void p_dir_free (PDir *dir) { if (dir == NULL) return; if (P_LIKELY (dir->search_handle != INVALID_HANDLE_VALUE)) { if (P_UNLIKELY (!FindClose (dir->search_handle))) P_ERROR ("PDir::p_dir_free: FindClose() failed"); } p_free (dir->orig_path); p_free (dir); }
static void ClearParser(void *func) { long i; extern int yp_continue; yp_continue = 0; HashClear(&literalTable); /* sets literalTable.maxItems==0 */ p_free(literalTypes); literalTypes= 0; reparsing= func; nYpReList= 0; p_free(ypReList); ypReList= 0; for (i=0 ; i<nConstants ; i++) if (constantTable[i].ops==&dataBlockSym) Unref(constantTable[i].value.db); nConstants= maxConstants= 0; p_free(constantTable); constantTable= 0; nextPC= 0; if (vmCodeSize > 1024L) vmCodeSize= 0; /* force realloc */ else memset(vmCode, 0, sizeof(Instruction)*vmCodeSize); previousOp= 0; wasUndecided= 0; nVariableRefs= nConstantRefs= nGotoTargets= 0; if (maxVariableRefs > 256L) maxVariableRefs= 0; /* force realloc */ if (maxConstantRefs > 256L) maxConstantRefs= 0; /* force realloc */ if (maxGotoTargets > 256L) maxGotoTargets= 0; /* force realloc */ nPos= nKey= nLocal= nTarget= 0; hasPosList= 0; stackDepth= maxStackDepth= didMaxDepth= 0; nextInc= 0; if (incCodeSize > 64L) incCodeSize= 0; /* force realloc */ loopDepth= 0; nextBSP= 0; if (breakStackSize > 16L) breakStackSize= 0; /* force realloc */ nMatrixMarkers= 0; if (maxMatrixMarkers > 16L) maxMatrixMarkers= 0; /* force realloc */ insideFunc= 0; }
static p_file *PushInclude(const char *filename, int fullparse) { p_file *file= 0; char *name= 0; long i; if (YIsAbsolute(filename)) { /* absolute pathname doesn't need any prefix */ file= open_include(filename, fullparse); if (!file) return 0; name= p_strcpy(filename); } else { char *tmp; for (i=0 ; i<=nYpPrefixes ; i++) { if (i<nYpPrefixes) { tmp= p_strncat(ypPrefixes[i], filename, 0); name= YExpandName(tmp); p_free(tmp); } else { /* this branch is probably a bug -- * if . is not on path probably should not find file... * maybe protects against empty path? */ name= YExpandName(filename); if (!YIsAbsolute(name)) break; } file= open_include(name, fullparse); if (file) break; p_free(name); } if (!file) return 0; } if (nYpIncludes>=maxYpIncludes) { int newSize= maxYpIncludes+4; ypIncludes= p_realloc(ypIncludes, sizeof(IncludeFile)*newSize); maxYpIncludes= newSize; } if (fullparse) ClearSourceList(name); ypIncludes[nYpIncludes].file= file; ypIncludes[nYpIncludes].filename= name; ypIncludes[nYpIncludes].lastLineRead= 0; ypIncludes[nYpIncludes++].index = -1; prevErrLine= -1; return file; }
P_LIB_API void p_dir_free (PDir *dir) { if (P_UNLIKELY (dir == NULL)) return; if (P_LIKELY (dir->dir != NULL)) { if (P_UNLIKELY (closedir (dir->dir) != 0)) P_ERROR ("PDir::p_dir_free: closedir() failed"); } p_free (dir->path); p_free (dir->orig_path); p_free (dir); }
P_LIB_API void p_hash_table_remove (PHashTable *table, pconstpointer key) { PHashTableNode *node, *prev_node; puint hash; if (P_UNLIKELY (table == NULL)) return; if (pp_hash_table_find_node (table, key) != NULL) { hash = pp_hash_table_calc_hash (key, table->size); node = table->table[hash]; prev_node = NULL; while (node != NULL) { if (node->key == key) { if (prev_node == NULL) table->table[hash] = node->next; else prev_node->next = node->next; p_free (node); break; } else { prev_node = node; node = node->next; } } } }
static void y_zap_hashtmp(void *ht) { y_hashtmp *h = ht; HashClear(&h->table); p_free(h); }
void GhDeletePalette(int n) { GpColorCell *palette= 0; if (n<0 || n>=GH_NDEVS) return; if (ghDevices[n].display) palette= ghDevices[n].display->palette; else if (ghDevices[n].hcp) palette= ghDevices[n].hcp->palette; if (palette) { int i; /* clear palette for this device */ if (ghDevices[n].display) GpSetPalette(ghDevices[n].display, (GpColorCell *)0, 0); if (ghDevices[n].hcp) GpSetPalette(ghDevices[n].hcp, (GpColorCell *)0, 0); /* free the palette if there are no other references to it */ for (i=0 ; i<GH_NDEVS ; i++) if ((ghDevices[i].display && ghDevices[i].display->palette==palette) || (ghDevices[i].hcp && ghDevices[i].hcp->palette==palette)) break; if (i>=GH_NDEVS) { if (hcpDefault && palette==hcpDefault->palette) GpSetPalette(hcpDefault, (GpColorCell *)0, 0); p_free(palette); } } }
int GaGetScratchP(long n) { if (n<=nScratchP) return 0; if (nScratchP>0) { p_free(gaxScratch); p_free(gayScratch); } gaxScratch= (GpReal *)p_malloc(sizeof(GpReal)*n); gayScratch= (GpReal *)p_malloc(sizeof(GpReal)*n); if (!gaxScratch || !gayScratch) { if (gaxScratch) p_free(gaxScratch); if (gayScratch) p_free(gayScratch); nScratchP= 0; MMError(); return 1; } nScratchP= n; return 0; }
static void clear_color_list() /* clear list of colors */ { struct clr_lst *tp; /* list-walking pointer */ struct clr_lst *tnxt; /* list-walking pointer */ int i; #ifdef TRACE if (debugger_trace) p_info(PI_TRACE, "clear_color_list\n"); if (color_trace) p_info(PI_TRACE, "clear_color_list ()\n"); #endif for (i=0; i<MAX_CLRS; i++) { tp = clr_1st[i]; clr_1st[i] = 0; while(tp != 0) /* walk towards the end */ { tnxt = tp->nxt; /* save location of next one */ p_free((char *)tp); /* free the memory */ #ifdef TRACE if (color_trace) p_info(PI_TRACE, "Plate #%d, %x->%d,%d,%.2f,%f,%s,%x removed\n", i,(unsigned)tp, tp->color, tp->freq, tp->density, tp->angle, tp->func, (unsigned)tp->nxt); #endif tp = tnxt; /* and point to 'next' */ } /* end while(tp != 0) */ } /* end for(i=0; i<MAX_CLRS; i++) */ } /* end function */
static int psckt_shutdown(psckt_t *sock) { psckt_t *s, *prev; if (!sock->finished) psckt_stop_thread(sock); if (sock->fd != INVALID_SOCKET) { closesocket(sock->fd); /* hope select() always returns after this? */ sock->fd = INVALID_SOCKET; } if (sock->thread != INVALID_HANDLE_VALUE) { /* wait for half second for thread to complete, then nuke it */ if (WaitForSingleObject(sock->thread, 500) != WAIT_OBJECT_0) TerminateThread(sock->thread, 6); CloseHandle(sock->thread); sock->thread = INVALID_HANDLE_VALUE; } if (sock->ready != INVALID_HANDLE_VALUE) { CloseHandle(sock->ready); sock->ready = INVALID_HANDLE_VALUE; } if (sock->released != INVALID_HANDLE_VALUE) { CloseHandle(sock->released); sock->released = INVALID_HANDLE_VALUE; } for (s=psckt_list,prev=0 ; s ; prev=s,s=s->next) if (s == sock) break; if (prev) prev->next = sock->next; else psckt_list = sock->next; if (sock->peer) { p_free(sock->peer); sock->peer = 0; } return -1; /* convenience for send, recv */ }
int p_fclose(p_file *file) { int flag = (file->binary&2)? _pclose(file->fp) : fclose(file->fp); p_free(file); return flag; }
static int GetScratch(long n) { if (n<=nScratch) return 0; if (nScratch>0) { p_free(xScratch); p_free(yScratch); } xScratch= (GpReal *)p_malloc(sizeof(GpReal)*n); yScratch= (GpReal *)p_malloc(sizeof(GpReal)*n); if (!xScratch || !yScratch) { if (xScratch) p_free(xScratch); if (yScratch) p_free(yScratch); nScratch= 0; MMError(); return 1; } nScratch= n; return 0; }
p_file *YpPop(void) { char *filename; p_file *file; if (nYpInputs<=0) return 0; filename= ypInputs[--nYpInputs]; file= YpPushInclude(filename); if (!file) { char *msg; msg= p_strncat("missing include file ", filename, 0); YpError(msg); p_free(msg); } p_free(filename); return file; }