/** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to * result. On error, log and terminate the process. (Same as malloc(size), * but never returns NULL.) */ void * tor_malloc_(size_t size) { void *result; raw_assert(size < SIZE_T_CEILING); #ifndef MALLOC_ZERO_WORKS /* Some libc mallocs don't work when size==0. Override them. */ if (size==0) { size=1; } #endif /* !defined(MALLOC_ZERO_WORKS) */ result = raw_malloc(size); if (PREDICT_UNLIKELY(result == NULL)) { /* LCOV_EXCL_START */ /* If these functions die within a worker process, they won't call * spawn_exit, but that's ok, since the parent will run out of memory soon * anyway. */ raw_assert_unreached_msg("Out of memory on malloc(). Dying."); /* LCOV_EXCL_STOP */ } return result; }
/* * Initialization of device registration information table */ static void init_device( void ) { DevCB *devcb; INT num; ER ercd; num = MaxRegDev; /* Generate device registration information table */ DevCBtbl = raw_malloc(num * sizeof(DevCB)); if (DevCBtbl == NULL) { RAW_ASSERT(0); } list_init(&UsedDevCB); list_init(&FreeDevCB); devcb = DevCBtbl; while ( num-- > 0 ) { list_insert(&FreeDevCB, &devcb->q); devcb->devnm[0] = '\0'; devcb++; } return RAW_SUCCESS; }
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void * (*start_routine)(void *), void *arg) { pthread_struct *thread_struct; void *stack_base; attr = attr; thread_struct = raw_malloc(sizeof(pthread_struct)); stack_base = raw_malloc(4 * DEFAULT_STACK_ELEMENT); *thread = thread_struct; raw_task_create(&thread_struct->task_obj, (RAW_U8 *)"task1", arg, 30, 0, stack_base, DEFAULT_STACK_ELEMENT, (RAW_TASK_ENTRY)start_routine, RAW_TASK_AUTO_START); raw_semaphore_create(&thread_struct->task_sem, "sem", 0); thread_struct->task_sem.common_block_obj.block_way = RAW_BLOCKED_WAY_FIFO; return 0; }
void *mdbg_malloc(const char *fname, int lineno, size_t size) { struct mdbg_hdr *hdr; uint32_t exceptions = malloc_lock(); /* * Check struct mdbg_hdr doesn't get bad alignment. * This is required by C standard: the buffer returned from * malloc() should be aligned with a fundamental alignment. * For ARM32, the required alignment is 8. For ARM64, it is 16. */ COMPILE_TIME_ASSERT( (sizeof(struct mdbg_hdr) % (__alignof(uintptr_t) * 2)) == 0); hdr = raw_malloc(sizeof(struct mdbg_hdr), mdbg_get_ftr_size(size), size); if (hdr) { mdbg_update_hdr(hdr, fname, lineno, size); hdr++; } malloc_unlock(exceptions); return hdr; }
static void *raw_memalign(size_t hdr_size, size_t ftr_size, size_t alignment, size_t size) { size_t s; uintptr_t b; raw_malloc_validate_pools(); if (!IS_POWER_OF_TWO(alignment)) return NULL; /* * Normal malloc with headers always returns something SizeQuant * aligned. */ if (alignment <= SizeQuant) return raw_malloc(hdr_size, ftr_size, size); s = hdr_size + ftr_size + alignment + size + SizeQ + sizeof(struct bhead); /* Check wapping */ if (s < alignment || s < size) return NULL; b = (uintptr_t)bget(s); if (!b) goto out; if ((b + hdr_size) & (alignment - 1)) { /* * Returned buffer is not aligned as requested if the * hdr_size is added. Find an offset into the buffer * that is far enough in to the buffer to be able to free * what's in front. */ uintptr_t p; /* * Find the point where the buffer including supplied * header size should start. */ p = b + hdr_size + alignment; p &= ~(alignment - 1); p -= hdr_size; if ((p - b) < (SizeQ + sizeof(struct bhead))) p += alignment; assert((p + hdr_size + ftr_size + size) <= (b + s)); /* Free the front part of the buffer */ brel_before((void *)b, (void *)p); /* Set the new start of the buffer */ b = p; } /* * Since b is now aligned, release what we don't need at the end of * the buffer. */ brel_after((void *)b, hdr_size + ftr_size + size); out: raw_malloc_return_hook((void *)b, size); return (void *)b; }
void test_page(void * pParam) { void *test1; void *test2; PAGETBL *pg; int count = 0; raw_page_init(test_page_mem, test_page_mem + 1024*1024); pg = system_page_table_get(); vc_port_printf("111 max page is %d, free page is %d, top page addr is %p\n", pg->maxpage, pg->freepage, pg->top_page); raw_malloc_init(); test1 = raw_malloc(188888); test2 = raw_malloc(18888); pg = system_page_table_get(); vc_port_printf("222 max page is %d, free page is %d, top page addr is %p\n", pg->maxpage, pg->freepage, pg->top_page); raw_free(test2); pg = system_page_table_get(); vc_port_printf("333 max page is %d, free page is %d, top page addr is %p\n", pg->maxpage, pg->freepage, pg->top_page); raw_free(test1); pg = system_page_table_get(); vc_port_printf("4444 max page is %d, free page is %d, top page addr is %p\n", pg->maxpage, pg->freepage, pg->top_page); while (1) { test1 = raw_page_allocate(25); if (test1 == 0) { vc_port_printf("RAW_NO_MEMORY\n"); RAW_ASSERT(0); } test2 = raw_page_allocate(25); if (test2 == 0) { vc_port_printf("RAW_NO_MEMORY\n"); RAW_ASSERT(0); } raw_page_free(test2); vc_port_printf("page success is %d\n", count++); } }