void * aresize(void *ptr, size_t numb, Area *ap) { ALLOC_ITEM *lp = NULL; /* resizing (true) or newly allocating? */ if (ptr != NULL) { ALLOC_ITEM *pp; pp = findptr(&lp, ptr, ap); pp->next = lp->next; } if ((numb >= SIZE_MAX - ALLOC_SIZE) || (lp = remalloc(lp, numb + ALLOC_SIZE)) == NULL #ifndef MKSH_SMALL || ALLOC_ISUNALIGNED(lp) #endif ) internal_errorf("cannot allocate %lu data bytes", (unsigned long)numb); /* this only works because Area is an ALLOC_ITEM */ lp->next = ap->next; ap->next = lp; /* return user item address */ return ((char *)lp + ALLOC_SIZE); }
reroot *remkroot(size_t size) { reroot *r = (reroot *)remalloc(sizeof(reroot), "recycle.c, root"); r->list = (recycle *)0; r->trash = (recycle *)0; r->size = align(size); r->logsize = RESTART; r->numleft = 0; return r; }
static void set_syscall_symbol (void) { char cfgbuf[CONFIG_MAX]; int ret; ret = get_config(pal_state.root_config, "loader.syscall_symbol", cfgbuf, CONFIG_MAX); if (ret <= 0) return; pal_state.syscall_sym_name = remalloc(cfgbuf, ret + 1); }
void deliver_signal (siginfo_t * info, PAL_CONTEXT * context) { shim_tcb_t * tcb = SHIM_GET_TLS(); struct shim_thread * cur_thread = (struct shim_thread *) tcb->tp; int sig = info->si_signo; __disable_preempt(tcb); struct shim_signal * signal = __alloca(sizeof(struct shim_signal)); /* save in signal */ memset(signal, 0, sizeof(struct shim_signal)); __store_info(info, signal); __store_context(tcb, context, signal); if ((tcb->context.preempt & ~SIGNAL_DELAYED) > 1) goto delay; if (__sigismember(&cur_thread->signal_mask, sig)) goto delay; __handle_signal(tcb, sig, &signal->context); __handle_one_signal(tcb, sig, signal); goto out; delay: { if (!(signal = remalloc(signal,sizeof(struct shim_signal)))) goto out; struct shim_signal ** signal_log = allocate_signal_log(cur_thread, sig); if (!signal_log) { sys_printf("signal queue is full (TID = %u, SIG = %d)\n", tcb->tid, sig); free(signal); goto out; } *signal_log = signal; } out: __enable_preempt(tcb); }
int get_file(t_pars *ppt, int fd) { char *line; int i; i = 0; if ((ppt->file = malloc(sizeof(char *) * 256)) == NULL) return (merror("malloc_error", -1)); while ((line = get_next_line(fd))) { if ((ppt->file[i] = my_strdup(line)) == NULL) return (merror("Malloc error\n", -1)); ++i; if (i % 255 == 0) if ((ppt->file = remalloc(ppt->file, i)) == NULL) return (merror("Malloc error\n", -1)); free(line); } ppt->file[i] = '\0'; return (0); }
/* to be called from the macro renew only */ char *renewx(struct reroot *r) { recycle *temp; if (r->trash) { /* pull a node off the trash heap */ temp = r->trash; r->trash = temp->next; (void)memset((void *)temp, 0, r->size); } else { /* allocate a new block of nodes */ r->numleft = r->size*((uint32_t)1<<r->logsize); if (r->numleft < REMAX) ++r->logsize; temp = (recycle *)remalloc(sizeof(recycle) + r->numleft, "recycle.c, data"); temp->next = r->list; r->list = temp; r->numleft-=r->size; temp = (recycle *)((char *)(r->list+1)+r->numleft); } return (char *)temp; }
int main(int argc, char *argv[]) { clock_t begin, end; float time_spent; int items = 1000; double size = 64; if ( argc != 2 ) { return 0; } else if (atoi(argv[1]) == 1) { memory_manager_init(); begin = clock(); for (int i = 0; i < items; ++i) { void *l = memory_manager_allocate(size); memory_manager_free(l); size++; } end = clock(); time_spent = ((float)(begin - end)) / CLOCKS_PER_SEC; printf("custom time: %lf \n", time_spent); memory_manager_shutdown(); } else if (atoi(argv[1]) == 2) { begin = clock(); for (int i = 0; i < items; ++i) { void *l = remalloc(size); free(l); size++; } end = clock(); time_spent = ((float)(begin - end)) / CLOCKS_PER_SEC; printf("malloc time: %lf \n", time_spent); } return 0; }
void pal_main (PAL_NUM pal_token, void * pal_addr, const char * pal_name, int argc, const char ** argv, const char ** envp, PAL_HANDLE parent_handle, PAL_HANDLE thread_handle, PAL_HANDLE exec_handle, PAL_HANDLE manifest_handle) { int ret; bool is_parent = !parent_handle; #if PROFILING == 1 __pal_control.host_specific_startup_time = _DkSystemTimeQuery() - pal_state.start_time; #endif pal_state.pal_token = pal_token; pal_state.pal_addr = pal_addr; pal_state.parent_handle = parent_handle; pal_state.pagesize = _DkGetPagesize(); pal_state.alloc_align = _DkGetAllocationAlignment(); pal_state.alloc_shift = pal_state.alloc_align - 1; pal_state.alloc_mask = ~pal_state.alloc_shift; init_slab_mgr(pal_state.alloc_align); char * exec = NULL, * manifest = NULL; if (exec_handle) { exec = __alloca(URI_MAX); ret = _DkStreamGetName(exec_handle, exec, URI_MAX); if (ret < 0) init_fail(-ret, "cannot get executable name"); } if (manifest_handle) { manifest = __alloca(URI_MAX); ret = _DkStreamGetName(manifest_handle, manifest, URI_MAX); if (ret < 0) init_fail(-ret, "cannot get manifest name"); } else { if (is_parent) { #if PROFILING == 1 unsigned long before_find_manifest = _DkSystemTimeQuery(); #endif do { if (exec_handle) { assert(!!exec); /* try open "<exec>.manifest" */ manifest = __alloca(URI_MAX); snprintf(manifest, URI_MAX, "%s.manifest", exec); ret = _DkStreamOpen(&manifest_handle, manifest, PAL_ACCESS_RDONLY, 0, 0, 0); if (!ret) break; } /* try open "file:manifest" */ manifest = "file:manifest"; ret = _DkStreamOpen(&manifest_handle, manifest, PAL_ACCESS_RDONLY, 0, 0, 0); if (!ret) break; /* well, there is no manifest file, leave it alone */ if (!manifest_handle) printf("Can't fine any manifest, will run without one\n"); } while (0); #if PROFILING == 1 pal_state.manifest_loading_time += _DkSystemTimeQuery() - before_find_manifest; #endif } } /* load manifest if there is one */ if (manifest_handle) { #if PROFILING == 1 unsigned long before_load_manifest = _DkSystemTimeQuery(); #endif PAL_STREAM_ATTR attr; ret = _DkStreamAttributesQuerybyHandle(manifest_handle, &attr); if (ret < 0) init_fail(-ret, "cannot open manifest file"); void * cfg_addr = NULL; int cfg_size = attr.pending_size; ret = _DkStreamMap(manifest_handle, &cfg_addr, PAL_PROT_READ, 0, ALLOC_ALIGNUP(cfg_size)); if (ret < 0) init_fail(-ret, "cannot open manifest file"); struct config_store * root_config = malloc(sizeof(struct config_store)); root_config->raw_data = cfg_addr; root_config->raw_size = cfg_size; root_config->malloc = malloc; root_config->free = free; const char * errstring = NULL; if ((ret = read_config(root_config, loader_filter, &errstring)) < 0) init_fail(-ret, errstring); pal_state.root_config = root_config; #if PROFILING == 1 pal_state.manifest_loading_time += _DkSystemTimeQuery() - before_load_manifest; #endif } /* if there is no executable, try to find one in the manifest */ if (is_parent && !exec_handle) { exec = __alloca(URI_MAX); assert(!!pal_state.root_config); ret = get_config(pal_state.root_config, "loader.exec", exec, URI_MAX); if (ret > 0) { ret = _DkStreamOpen(&exec_handle, exec, PAL_ACCESS_RDONLY, 0, 0, 0); if (ret < 0) init_fail(-ret, "cannot open executable"); /* must be a ELF */ if (check_elf_object(exec_handle) < 0) init_fail(PAL_ERROR_INVAL, "executable is not a ELF binary"); } else { exec = NULL; } } if (is_parent && !exec_handle && !manifest_handle) { printf("USAGE: %s [executable|manifest] args ...\n", pal_name); _DkProcessExit(0); return; } pal_state.manifest = manifest; pal_state.manifest_handle = manifest_handle; pal_state.exec = exec; pal_state.exec_handle = exec_handle; const char * first_argv = *argv; argc--; argv++; if (is_parent && exec_handle) { first_argv = exec; if (pal_state.root_config) { char cfgbuf[CONFIG_MAX]; ret = get_config(pal_state.root_config, "loader.execname", cfgbuf, CONFIG_MAX); if (ret > 0) first_argv = remalloc(cfgbuf, ret + 1); } } if (pal_state.root_config) load_libraries(); if (exec_handle) { #if PROFILING == 1 unsigned long before_load_exec = _DkSystemTimeQuery(); #endif ret = load_elf_object_by_handle(exec_handle, OBJECT_EXEC); if (ret < 0) init_fail(ret, PAL_STRERROR(ret)); #if PROFILING == 1 pal_state.linking_time += _DkSystemTimeQuery() - before_load_exec; #endif } #if PROFILING == 1 unsigned long before_tail = _DkSystemTimeQuery(); #endif if (pal_state.root_config) { read_envs(&envp); set_debug_type(); set_syscall_symbol(); } __pal_control.process_id = _DkGetProcessId(); __pal_control.host_id = _DkGetHostId(); __pal_control.manifest_handle = manifest_handle; __pal_control.executable = exec; __pal_control.parent_process = parent_handle; __pal_control.first_thread = thread_handle; _DkGetAvailableUserAddressRange(&__pal_control.user_address.start, &__pal_control.user_address.end); __pal_control.pagesize = pal_state.pagesize; __pal_control.alloc_align = pal_state.alloc_align; __pal_control.broadcast_stream = _DkBroadcastStreamOpen(); _DkGetCPUInfo(&__pal_control.cpu_info); __pal_control.mem_info.mem_total = _DkMemoryQuota(); #if PROFILING == 1 pal_state.tail_startup_time += _DkSystemTimeQuery() - before_tail; __pal_control.relocation_time = pal_state.relocation_time; __pal_control.linking_time = pal_state.linking_time; __pal_control.manifest_loading_time = pal_state.manifest_loading_time; __pal_control.allocation_time = pal_state.slab_time; __pal_control.child_creation_time = is_parent ? 0 : pal_state.start_time - pal_state.process_create_time; #endif /* Now we will start the execution */ start_execution(first_argv, argc, argv, envp); /* We wish we will never reached here */ init_fail(PAL_ERROR_DENIED, "unexpected termination"); }