PVOID NTAPI GetSystemInformation ( SYSTEM_INFORMATION_CLASS InfoClass ) { NTSTATUS Status; PVOID Buffer; ULONG Size = PAGE_SIZE; do { Buffer = halloc (Size); Status = ZwQuerySystemInformation ( InfoClass, Buffer, Size, &Size ); if (Status == STATUS_INFO_LENGTH_MISMATCH) hfree (Buffer); } while (Status == STATUS_INFO_LENGTH_MISMATCH); if (!NT_SUCCESS(Status)) { hfree (Buffer); return NULL; } return Buffer; }
int rotate_log(void) { char *tmp; int i; char *r1, *r2; if (rwl_trywrlock(&log_file_lock)) { fprintf(stderr, "failed to wrlock log_file_lock for rotation\n"); return 0; } // check if still oversize and not rotated by another thread off_t l = lseek(log_file, 0, SEEK_CUR); if (l < log_rotate_size) { rwl_wrunlock(&log_file_lock); return 0; } // rename tmp = hmalloc(strlen(log_fname) + 6); sprintf(tmp, "%s.tmp", log_fname); if (rename(log_fname, tmp) != 0) { fprintf(stderr, "aprsc logger: Failed to rename %s to %s: %s\n", log_fname, tmp, strerror(errno)); // continue anyway, try to reopen } // reopen if (close(log_file)) fprintf(stderr, "aprsc logger: Could not close log file %s: %s\n", log_fname, strerror(errno)); log_file = open(log_fname, O_WRONLY|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR|S_IRGRP); if (log_file < 0) { fprintf(stderr, "aprsc logger: Could not open %s: %s\n", log_fname, strerror(errno)); log_file = -1; } rwl_wrunlock(&log_file_lock); // do the rest of the rotation r1 = hmalloc(strlen(log_fname) + 16); r2 = hmalloc(strlen(log_fname) + 16); for (i = log_rotate_num-1; i > 0; i--) { sprintf(r1, "%s.%d", log_fname, i-1); sprintf(r2, "%s.%d", log_fname, i); if (rename(r1, r2) != 0 && errno != ENOENT) { fprintf(stderr, "rename %s => %s failed:%s\n", r1, r2, strerror(errno)); } } if (rename(tmp, r1) != 0) { fprintf(stderr, "aprsc logger: Failed to rename %s to %s: %s\n", tmp, r1, strerror(errno)); } hfree(tmp); hfree(r1); hfree(r2); return 0; }
int wmain(int argc, LPWSTR *argv) { LPSTR oldname; LPWSTR newname; int strlength; while (--argc > 0) { ++argv; strlength = (int) wcslen(argv[0])+1; oldname = halloc_seh(strlength*sizeof(*oldname)); WideCharToMultiByte(CP_ACP, 0, argv[0], -1, oldname, strlength, NULL, NULL); newname = halloc_seh(strlength*sizeof(*newname)); MultiByteToWideChar(CP_OEMCP, 0, oldname, -1, newname, strlength); if (MoveFile(argv[0], newname)) printf("'%ws' -> '%ws', OK.\n", argv[0], newname); else win_perror(newname); hfree(oldname); hfree(newname); } return 0; }
int loadlbm(char *fname) { int x,y,a,b,c; long l; char far *filebuf; unsigned u; int firstone=1; filebuf=halloc(32768L,1L); printf("\nLoading and uncompressing picture (LBM): "); if(filebuf==NULL) { printf("Out of memory.\n"); return(1); } { f1=fopen(fname,"rb"); if(f1==NULL) return(3); setvbuf(f1,filebuf,_IOFBF,32768); do { memcpy(lasttype,type,5); /* get header */ do { type[0]=getc(f1); } while(type[0]==0 && !feof(f1)); type[1]=getc(f1); type[2]=getc(f1); type[4]=0; type[3]=getc(f1); len=getc(f1)*256L*65536L; if(firstone && memcmp(type,"FORM",4)) return(2); firstone=0; len+=getc(f1)*65536L; len+=getc(f1)*256L; len+=getc(f1); /* process it */ a=loadlbm2(); if(errtxt!=NULL) { printf("%s\n",errtxt); hfree(filebuf); return(1); } if(a==-1) { printf("Not a Deluxepaint file!"); return(2); } if(a==2) { printf("OLD:<%s> NEW<%s>\n",lasttype,type); hfree(filebuf); return(1); } else if(a==1) break; } while(!feof(f1) && !eschit()); fclose(f1); } hfree(filebuf); if(kbhit()) return(1); return(0); }
void status_atend(void) { struct cdata_list_t *cl, *cl_next; int pe; for (cl = cdata_list; (cl); cl = cl_next) { cl_next = cl->next; cdata_free(cl->cd); hfree(cl); } if ((pe = pthread_mutex_lock(&status_json_mt))) { hlog(LOG_ERR, "status_atend(): could not lock status_json_mt: %s", strerror(pe)); return; } if (status_json_cached) { hfree(status_json_cached); status_json_cached = NULL; } if ((pe = pthread_mutex_unlock(&status_json_mt))) { hlog(LOG_ERR, "status_atend(): could not unlock status_json_mt: %s", strerror(pe)); return; } }
void cdata_free(struct cdata_t *cd) { if (cd->next) cd->next->prevp = cd->prevp; *cd->prevp = cd->next; hfree(cd->name); hfree(cd); }
int status_read_liveupgrade(void) { char path[PATHLEN+1]; char path_renamed[PATHLEN+1]; FILE *fp; char *s = NULL; int sl = 0; char buf[32768]; int i; snprintf(path, PATHLEN, "%s/liveupgrade.json", rundir); snprintf(path_renamed, PATHLEN, "%s/liveupgrade.json.old", rundir); fp = fopen(path, "r"); if (!fp) { hlog(LOG_ERR, "liveupgrade dump file read failed: Could not open %s for reading: %s", path, strerror(errno)); return -1; } hlog(LOG_INFO, "Live upgrade: Loading client status from %s ...", path); if (rename(path, path_renamed) < 0) { hlog(LOG_ERR, "Failed to rename liveupgrade dump file %s to %s: %s", path, path_renamed, strerror(errno)); unlink(path); } while ((i = fread(buf, 1, sizeof(buf), fp)) > 0) { //hlog(LOG_DEBUG, "read %d bytes", i); s = hrealloc(s, sl + i+1); memcpy(s + sl, buf, i); sl += i; s[sl] = 0; // keep null-terminated //hlog(LOG_DEBUG, "now: %s", s); } if (fclose(fp)) { hlog(LOG_ERR, "liveupgrade dump file read failed: close(%s): %s", path, strerror(errno)); hfree(s); return -1; } /* decode JSON */ cJSON *dec = cJSON_Parse(s); if (!dec) { hlog(LOG_ERR, "liveupgrade dump parsing failed"); hfree(s); return -1; } liveupgrade_status = dec; hfree(s); return 0; }
void free_uplink_config(struct uplink_config_t **lc) { struct uplink_config_t *this; while (*lc) { this = *lc; *lc = this->next; hfree((void*)this->name); hfree((void*)this->url); hfree(this); } }
static int free_filemap(struct filemap *fm) { int rc; hunprotect(fm->file); rc = hfree(fm->file); if (rc < 0) return rc; rmap_free(vmap, BTOP(fm->addr), PAGES(fm->size)); hunprotect(fm->self); rc = hfree(fm->self); if (rc < 0) return rc; return 0; }
void os_free(struct os *os) { int i; #ifdef HAS_HTAB if (!os->cached_partition_info[1].sfw_tlb) { htab_free(os); } #endif /* HAS_HTAB */ if (os->po_psm) os_psm_destroy(os->po_psm); for (i = 0; i < os->installed_cpus; i++) { cpu_free(os->cpu[i]); os->cpu[i] = NULL; } ht_remove(&os_hash, os->po_lpid); os->po_lpid = -1; hfree(os, sizeof(*os)); }
void free_receiver(struct receiver *rx) { if (rx) { filter_free(rx->filter); hfree(rx); } }
uval ofd_cleanup(uval ofd) { uval space = ALIGN_UP(ofd_space(ofd), PGSIZE); hfree((void*)ofd, space); return 0; }
/** * Free routine for namespace name/value pairs. */ static void xfmt_nv_free(void *p, size_t unused_len) { (void) unused_len; hfree(p); }
/** * Build a QHT PATCH message. * * @param seqno the patch sequence number * @param seqsize the total length of the sequence * @param compressed whether patch is compressed * @param bits amount of bits for each entry (1) * @param buf start of patch data * @param len length in byte of patch data * * @return a /QHT message with a PATCH payload. */ pmsg_t * g2_build_qht_patch(int seqno, int seqsize, bool compressed, int bits, char *buf, int len) { g2_tree_t *t; char body[5]; /* The start of the payload */ void *payload, *p; pmsg_t *mb; g_assert(1 == bits); /* Only 1-bit patches in G2 */ p = payload = halloc(len + sizeof body); p = poke_u8(p, G2_QHT_PATCH); p = poke_u8(p, seqno); p = poke_u8(p, seqsize); p = poke_u8(p, compressed ? 0x1 : 0x0); p = poke_u8(p, bits); memcpy(p, buf, len); t = g2_tree_alloc(G2_NAME(QHT), payload, len + sizeof body); mb = g2_build_pmsg(t); g2_tree_free_null(&t); hfree(payload); return mb; }
void load_syms(void) { char sympath[256]; char errbuf[256]; snprintf(sympath, sizeof(sympath), "%s/showtime.syms", showtime_dataroot()); my_trace("sympath: %s\n", sympath); fa_handle_t *fh = fa_open(sympath, errbuf, sizeof(errbuf)); if(fh == NULL) { my_trace("Unable to open symbol file %s -- %s", sympath, errbuf); return; } int size = fa_fsize(fh); char *buf = halloc(size + 1); int r = fa_read(fh, buf, size); if(r != size) { my_trace("Unable to read %d bytes", size); hfree(buf, size+1); } else { buf[size] = 0; my_trace("Loaded symbol table %d bytes to %p", size, buf); symbuf = buf; } fa_close(fh); }
void io_libraryRelease(io_library_t *library) { spinlock_lock(&library->lock); if((-- library->refCount) == 0) { struct io_dependency_s *dependency = list_first(library->dependencies); while(dependency) { io_libraryRelease(dependency->library); dependency = dependency->next; } io_storeRemoveLibrary(library); if(library->vmemory) vm_free(vm_getKernelDirectory(), library->vmemory, library->pages); if(library->pmemory) pm_free(library->pmemory, library->pages); hfree(NULL, library->path); list_destroy(library->dependencies); return; } spinlock_unlock(&library->lock); }
io_module_t *__io_moduleCreateWithLibrary(io_library_t *library, bool retainLibrary) { io_module_t *module = halloc(NULL, sizeof(io_module_t)); if(module) { module->library = library; module->module = NULL; module->name = library->name; module->references = 1; module->lock = SPINLOCK_INIT; module->start = (io_module_start_t)io_libraryFindSymbol(library, "_kern_start"); module->stop = (io_module_stop_t)io_libraryFindSymbol(library, "_kern_stop"); if(!module->start || !module->stop) { dbg("library %s doesn't provide kernel entry points!\n", library->name); hfree(NULL, module); return NULL; } if(retainLibrary) io_libraryRetain(library); atree_insert(__io_moduleTree, module, module->name); } return module; }
/* Free memory allocated by a png_create_struct() call */ void /* PRIVATE */ png_destroy_struct_2(png_voidp struct_ptr, png_free_ptr free_fn, png_voidp mem_ptr) { #endif /* PNG_USER_MEM_SUPPORTED */ if (struct_ptr != NULL) { #ifdef PNG_USER_MEM_SUPPORTED if (free_fn != NULL) { png_struct dummy_struct; png_structp png_ptr = &dummy_struct; png_ptr->mem_ptr=mem_ptr; (*(free_fn))(png_ptr, struct_ptr); return; } #endif /* PNG_USER_MEM_SUPPORTED */ #if defined(__TURBOC__) && !defined(__FLAT__) farfree(struct_ptr); #else # if defined(_MSC_VER) && defined(MAXSEG_64K) hfree(struct_ptr); # else free(struct_ptr); # endif #endif } }
/** * Format data to stream. * * @return the amount of bytes written, -1 on error. */ ssize_t ostream_printf(ostream_t *os, const char *fmt, ...) { va_list args, args2; char buf[1024]; size_t len; char *data; ssize_t w; ostream_check(os); va_start(args, fmt); VA_COPY(args2, args); len = gm_vsnprintf(buf, sizeof buf, fmt, args2); va_end(args2); if (len >= sizeof buf - 1) { data = h_strdup_len_vprintf(fmt, args, &len); } else { data = buf; } va_end(args); w = ostream_write(os, data, len); if (data != buf) hfree(data); return w; }
void savehist(struct wordent *sp) { struct Hist *hp, *np; Char *cp; int histlen; histlen = 0; /* throw away null lines */ if (sp->next->word[0] == '\n') return; cp = value(STRhistory); if (*cp) { Char *p = cp; while (*p) { if (!Isdigit(*p)) { histlen = 0; break; } histlen = histlen * 10 + *p++ - '0'; } } for (hp = &Histlist; (np = hp->Hnext) != NULL;) if (eventno - np->Href >= histlen || histlen == 0) hp->Hnext = np->Hnext, hfree(np); else hp = np; (void) enthist(++eventno, sp, 1); }
time_t parse_interval(char *origs) { time_t t = 0; int i; char *s, *np, *p, c; np = p = s = hstrdup(origs); while (*p) { if (!isdigit((int)*p)) { c = tolower(*p); *p = '\0'; i = atoi(np); if (c == 's') t += i; else if (c == 'm') t += 60 * i; else if (c == 'h') t += 60 * 60 * i; else if (c == 'd') t += 24 * 60 * 60 * i; np = p + 1; } p++; } if (*np) t += atoi(np); hfree(s); return t; }
void ssl_free(struct ssl_t *ssl) { if (ssl->ctx) SSL_CTX_free(ssl->ctx); hfree(ssl); }
/** * Extract interest flags from the payload of a /Q2/I tree node. * * @return the consolidated flags G2_Q2_F_* requested by the payload. */ static uint32 g2_node_extract_interest(const g2_tree_t *t) { const char *p, *q, *end; size_t paylen; uint32 flags = 0; p = q = g2_tree_node_payload(t, &paylen); if (NULL == p) return 0; end = p + paylen; while (q != end) { if ('\0' == *q++) { flags |= TOKENIZE(p, g2_q2_i); p = q; } } if (p != q) { char *r = h_strndup(p, q - p); /* String not NUL-terminated */ flags |= TOKENIZE(r, g2_q2_i); hfree(r); } return flags; }
/** * Search executable within the user's PATH. * * @return full path if found, NULL otherwise. * The returned string is allocated with halloc(). */ char * file_locate_from_path(const char *argv0) { static bool already_done; char *path; char *tok; char filepath[MAX_PATH_LEN + 1]; char *result = NULL; char *ext = ""; if (is_running_on_mingw() && !is_strsuffix(argv0, (size_t) -1, ".exe")) { ext = ".exe"; } if (filepath_basename(argv0) != argv0) { if (!already_done) { s_warning("can't locate \"%s\" in PATH: name contains '%c' already", argv0, strchr(argv0, G_DIR_SEPARATOR) != NULL ? G_DIR_SEPARATOR : '/'); } goto done; } path = getenv("PATH"); if (NULL == path) { if (!already_done) { s_warning("can't locate \"%s\" in PATH: " "no such environment variable", argv0); } goto done; } path = h_strdup(path); tok = strtok(path, G_SEARCHPATH_SEPARATOR_S); while (NULL != tok) { const char *dir = tok; filestat_t buf; if ('\0' == *dir) dir = "."; concat_strings(filepath, sizeof filepath, dir, G_DIR_SEPARATOR_S, argv0, ext, NULL); if (-1 != stat(filepath, &buf)) { if (S_ISREG(buf.st_mode) && -1 != access(filepath, X_OK)) { result = h_strdup(filepath); break; } } tok = strtok(NULL, G_SEARCHPATH_SEPARATOR_S); } hfree(path); done: already_done = TRUE; /* No warning on subsequent invocation */ return result; }
static int status_dump_fp(FILE *fp) { char *out = status_json_string(1, 1); fputs(out, fp); hfree(out); return 0; }
static void __psm_destroy(struct rcu_node *rcu) { struct page_share_mgr *psm; psm = PARENT_OBJ(struct page_share_mgr, psm_lci.lci_rcu, rcu); hfree(psm, sizeof (*psm)); }
/** * Initialize internal state for our incremental zlib stream. * * @param zs the zlib stream structure to initialize * @param data input data to process; if NULL, will be incrementally given * @param len length of data to process (if data not NULL) or estimation * @param dest where processed data should go, or NULL if allocated * @param destlen length of supplied output buffer, if dest != NULL */ static void zlib_stream_init(zlib_stream_t *zs, const void *data, size_t len, void *dest, size_t destlen) { z_streamp z; g_assert(size_is_non_negative(len)); g_assert(size_is_non_negative(destlen)); zs->in = data; zs->inlen = data ? len : 0; zs->inlen_total = zs->inlen; /* * When deflating zlib requires normally 0.1% more + 12 bytes, we use * 0.5% to be safe. * * When inflating, assume we'll double the input at least. */ if (NULL == dest) { /* Processed data go to a dynamically allocated buffer */ if (!zs->allocated) { if (data == NULL && len == 0) len = 512; if (ZLIB_DEFLATER_MAGIC == zs->magic) { zs->outlen = (len * 1.005 + 12.0); g_assert(zs->outlen > len); g_assert(zs->outlen - len >= 12); } else { zs->outlen = UNSIGNED(len) * 2; } zs->out = halloc(zs->outlen); zs->allocated = TRUE; } } else { /* Processed data go to a supplied buffer, not-resizable */ if (zs->allocated) hfree(zs->out); zs->out = dest; zs->outlen = destlen; zs->allocated = FALSE; } /* * Initialize Z stream. */ z = zs->z; g_assert(z != NULL); /* Stream not closed yet */ z->next_out = zs->out; z->avail_out = zs->outlen; z->next_in = deconstify_pointer(zs->in); z->avail_in = 0; /* Will be set by zlib_xxx_step() */ }
void ssl_free_connection(struct client_t *c) { if (!c->ssl_con) return; SSL_free(c->ssl_con->connection); hfree(c->ssl_con); c->ssl_con = NULL; }
int do_string(char **dest, int argc, char **argv) { if (argc < 2) return -1; if (*dest) hfree(*dest); *dest = hstrdup(argv[1]); return 0; }
void array_removeAllObjects(array_t *array) { hfree(NULL, array->data); array->count = 0; array->space = 5; array->data = halloc(NULL, array->space * sizeof(void *)); }