extern BOOL table_create(table_t *tableReturn, size_t length) { table_t table; size_t i; assert(tableReturn != NULL); table = alloc_obj(sizeof(table_s)); if (table == NULL) goto failMallocTable; table->length = length; table->count = 0; table->array = alloc_obj(sizeof(table_entry_s) * length); if (table->array == NULL) goto failMallocArray; for (i = 0; i < length; ++i) { table->array[i].key = 0; table->array[i].value = NULL; table->array[i].status = TABLE_UNUSED; } *tableReturn = table; return TRUE; failMallocArray: free_obj(table, sizeof(table_s)); failMallocTable: return FALSE; }
int main() { printf("init result:%d\n", init_bitmap(34, &bitmap_objects)); dump_bitmap(&bitmap_objects); printf("firt set result:%d\n", first_set_index(&bitmap_objects)); OBJECT_TYPE *obj = NULL; for (int i = 0; i < bitmap_objects.num_of_objs; ++i) { //for (int i = 0; i < 30; ++i) { obj = alloc_obj(&bitmap_objects); } dump_bitmap(&bitmap_objects); printf("firt set result:%d\n", first_set_index(&bitmap_objects)); obj = bitmap_objects.objects; //for (int i = 0; i < bitmap_objects.num_of_objs; ++i) { for (int i = 0; i < 30; ++i) { free_obj(&bitmap_objects, obj++); } printf("firt set result:%d\n", first_set_index(&bitmap_objects)); dump_bitmap(&bitmap_objects); printf("firt set result:%d\n", first_set_index(&bitmap_objects)); free_obj(&bitmap_objects, obj++); free_obj(&bitmap_objects, obj++); free_obj(&bitmap_objects, obj++); free_obj(&bitmap_objects, obj++); printf("firt set result:%d\n", first_set_index(&bitmap_objects)); printf("uninit result:%d\n", uninit_bitmap(&bitmap_objects)); }
object *make_int(int value) { object *obj = alloc_obj(); obj->type = scm_int; obj->data.i = value; return obj; }
object *make_char(char c) { object *obj = alloc_obj(); obj->type = scm_char; obj->data.c = c; return obj; }
LISP_OBJ_PTR divide(LISP_OBJ_PTR args) { LISP_OBJ_PTR res = alloc_obj(); float float_res = 0; // for now, this always coerces to a float form(res) = FLOAT_FORM; if (is_float(car(args))) { float_res = float_value(car(args)); } else float_res = int_value(car(args)); args = cdr(args); if (args == nil_ptr) { float_value(res) = 1 / float_res; return res; } // TODO: check for zero division while (args != nil_ptr) { if (is_float(car(args))) float_res /= float_value(car(args)); else float_res /= int_value(car(args)); args = cdr(args); } float_value(res) = float_res; return res; }
LISP_OBJ_PTR mult(LISP_OBJ_PTR args) { LISP_OBJ_PTR res = alloc_obj(); int int_res = 1; float float_res = 1; BOOLEAN is_float = FALSE; while (args != nil_ptr) { // check to see if we should be adding floats if (is_float(car(args)) && !is_float) { float_res = int_res; is_float = TRUE; } // grab the proper number if (is_float(car(args))) { float_res *= float_value(car(args)); } else if (!is_float) int_res *= int_value(car(args)); else float_res *= int_value(car(args)); args = cdr(args); } if (is_float) { form(res) = FLOAT_FORM; float_value(res) = float_res; } else { form(res) = INT_FORM; int_value(res) = int_res; } return res; }
static BOOL table_grow(table_t table) { table_entry_t oldArray, newArray; size_t i, oldLength, newLength; oldLength = table->length; oldArray = table->array; newLength = table->length * 2; newArray = alloc_obj(sizeof(table_entry_s) * newLength); if (newArray == NULL) return FALSE; for (i = 0; i < newLength; ++i) { newArray[i].key = 0; newArray[i].value = NULL; newArray[i].status = TABLE_UNUSED; } table->length = newLength; table->array = newArray; for (i = 0; i < oldLength; ++i) { table_entry_t entry; assert(oldArray[i].status == TABLE_ACTIVE); /* should be full */ entry = table_find(table, oldArray[i].key, 0 /* none deleted */); assert(entry->status == TABLE_UNUSED); /* shouldn't be defined yet */ entry->key = oldArray[i].key; entry->value = oldArray[i].value; entry->status = TABLE_ACTIVE; } free_obj(oldArray, sizeof(table_entry_s) * oldLength); return TRUE; }
/* size is a multiple of SGEN_ALLOC_ALIGN */ static void* major_alloc_small_pinned_obj (size_t size, gboolean has_references) { void *res; ms_wait_for_sweep_done (); res = alloc_obj (size, TRUE, has_references); /*If we failed to alloc memory, we better try releasing memory *as pinned alloc is requested by the runtime. */ if (!res) { sgen_collect_major_no_lock ("pinned alloc failure"); res = alloc_obj (size, TRUE, has_references); } return res; }
inline static void push(Object arg, int pid) { volatile ListNode *n = alloc_obj(&pool_node); n->value = (Object)arg; clhLock(lhead, pid); // Critical section n->next = Head; Head = n; clhUnlock(lhead, pid); }
VCSI_OBJECT cons(VCSI_CONTEXT vc, VCSI_OBJECT x, VCSI_OBJECT y) { VCSI_OBJECT new_obj = alloc_obj(vc,CONS); CAR(new_obj) = x; CDR(new_obj) = y; return new_obj; }
Cute H_call( Cute id, Cute args ) { Cute p = alloc_obj( T_CALL ); N_call_id(p) = id; N_call_args(p) = args; return p; }
object *cons(object *car, object *cdr) { object *obj = alloc_obj(); obj->type = scm_pair; obj->data.pair.car = car; obj->data.pair.cdr = cdr; car->refs++; cdr->refs++; return obj; }
inline static RetVal serialEnqueue(void *state, ArgVal arg, int pid) { CCQueueStruct *st = (CCQueueStruct *)state; Node *node; node = alloc_obj(&pool_node); node->next = null; node->val = arg; st->last->next = node; st->last = node; return -1; }
object *make_str(char *str) { object *obj = alloc_obj(); obj->type = scm_str; obj->data.str = strdup(str); if (obj->data.str == NULL){ fprintf(stderr, "Out of memory.\n"); exit(1); } return obj; }
/* * size is already rounded up and we hold the GC lock. */ static void* major_alloc_degraded (MonoVTable *vtable, size_t size) { void *obj; int old_num_sections = num_major_sections; obj = alloc_obj (size, FALSE, vtable->klass->has_references); *(MonoVTable**)obj = vtable; HEAVY_STAT (++stat_objects_alloced_degraded); HEAVY_STAT (stat_bytes_alloced_degraded += size); g_assert (num_major_sections >= old_num_sections); mono_sgen_register_major_sections_alloced (num_major_sections - old_num_sections); return obj; }
static Cute eval_class( Cute p, Cute env ) { Cute class_env = N_class_env(p); // メソッドが使う環境をつくる Cute new_env = alloc_obj( T_FRAME ); N_frame_parent(new_env) = env; N_frame_dict(new_env) = N_frame_dict(class_env); // dictを共有する eval( N_class_body(p), new_env ); return p; }
inline void LFStackPush(LFStack *l, LFStackThreadState *th_state, ArgVal arg) { Node *n; n = alloc_obj(&th_state->pool); reset_backoff(&th_state->backoff); n->val = arg; do { Node *old_top = (Node *) l->top; // top is volatile n->next = old_top; if (CASPTR(&l->top, old_top, n) == true) break; else backoff_delay(&th_state->backoff); } while(true); }
LISP_OBJ_PTR make_lambda(ENVIRONMENT_PTR env, LISP_OBJ_PTR args) { // this code is a bit wet, we should combine with the define function LISP_OBJ_PTR fun, params; params = car(args); fun = alloc_obj(); form(fun) = PROCEDURE_FORM; proc_type(fun) = DERIVED; proc_env(fun) = env; proc_reqparams(fun) = params; proc_optparams(fun) = nil_ptr; proc_restparams(fun) = nil_ptr; proc_body(fun) = cdr(args); return fun; }
Object alloc_CudaFloatArray(int n) { if (!CudaFloatArray) { CudaFloatArray = alloc_class("CudaFloatArray", 4); add_Method(CudaFloatArray, "at", &cuda_FloatArray_at); add_Method(CudaFloatArray, "at()put", &cuda_FloatArray_at_put); add_Method(CudaFloatArray, "[]", &cuda_FloatArray_at); add_Method(CudaFloatArray, "[]:=", &cuda_FloatArray_at_put); } Object o = alloc_obj(sizeof(struct CudaFloatArray) - sizeof(struct Object) + sizeof(float) * n, CudaFloatArray); struct CudaFloatArray *a = (struct CudaFloatArray *)o; a->size = n; for (int i = 0; i < n; i++) { a->data[i] = 0.0f; } return o; }
struct scroll * make_scroll () { struct scroll * s = alloc_obj (sizeof (struct scroll), &scroll_ops); char bank = alloc_bank (); char old_blk5 = *ULTIMEM_BLK5; if (!bank) print_error ("Can't allocate bank for scroll."); // Clear scroll. s->bank = bank; *ULTIMEM_BLK5 = s->bank; bzero (0xa000, 0x2000); *ULTIMEM_BLK5 = old_blk5; return s; }
void ATTR_EXPORT test_mm(void) { VERBOSE(SYSTEM, "--- allocing continuous memory spaces\n"); void * cont_space = alloc_cont_space(12345); memset(cont_space, '\0', 12345); free_cont_space(cont_space); VERBOSE(SYSTEM, "--- allocing objects\n"); struct obj_page_head * obj_pages = NULL; for (int i = 0; i < 1000; i++) { void * ptr = alloc_obj(&obj_pages, i * 10); memset(ptr, '\0', i * 10); } clear_obj_pages(&obj_pages); VERBOSE(SYSTEM, "obj_pages become %p\n", obj_pages); }
/* * size is already rounded up and we hold the GC lock. */ static void* major_alloc_degraded (MonoVTable *vtable, size_t size) { void *obj; int old_num_sections; ms_wait_for_sweep_done (); old_num_sections = num_major_sections; obj = alloc_obj (size, FALSE, SGEN_VTABLE_HAS_REFERENCES (vtable)); if (G_LIKELY (obj)) { *(MonoVTable**)obj = vtable; HEAVY_STAT (++stat_objects_alloced_degraded); HEAVY_STAT (stat_bytes_alloced_degraded += size); g_assert (num_major_sections >= old_num_sections); mono_sgen_register_major_sections_alloced (num_major_sections - old_num_sections); } return obj; }
SmartCity ( const char * osm_file, const char * shm_segment ) { AdjacencyList alist, palist; std::size_t estimated_size; try { #ifdef DEBUG auto start = std::chrono::high_resolution_clock::now(); #endif OSMReader osm_reader ( osm_file, alist, palist, m_waynode_locations, m_busWayNodesMap, m_way2nodes ); estimated_size = 20*3*osm_reader.get_estimated_memory(); #ifdef DEBUG std::cout << " Processing OSM: " << std::chrono::duration_cast<std::chrono::milliseconds> ( std::chrono::high_resolution_clock::now() - start ).count() << " ms " << std::endl; #endif } catch ( std::exception &err ) { m_cv.notify_one(); m_run = false; m_thread.join(); throw; } google::protobuf::ShutdownProtobufLibrary(); m_remover = new shm_remove ( shm_segment ); segment = new boost::interprocess::managed_shared_memory ( boost::interprocess::create_only, shm_segment, estimated_size ); void_allocator alloc_obj ( segment->get_segment_manager() ); shm_map_Type* shm_map_n = segment->construct<shm_map_Type> ( "JustineMap" ) ( std::less<unsigned int>(), alloc_obj ); try { for ( AdjacencyList::iterator iter=alist.begin(); iter!=alist.end(); ++iter ) { SharedData v ( alloc_obj ); /* v.lon = m_waynode_locations.get ( iter->first ).x(); v.lat = m_waynode_locations.get ( iter->first ).y(); */ v.lon = m_waynode_locations[ iter->first ].x(); v.lat = m_waynode_locations[ iter->first ].y(); for ( WayNodesVect::iterator noderefi = iter->second.begin(); noderefi!= iter->second.end(); ++noderefi ) { v.m_alist.push_back ( *noderefi ); v.m_salist.push_back ( 0u ); v.m_palist.push_back ( palist[iter->first][std::distance ( iter->second.begin(), noderefi )]+1 ); } map_pair_Type p ( iter->first, v ); shm_map_n->insert ( p ); } #ifdef DEBUG std::cout << " alist.size = " << alist.size() << " (deg- >= 1)"<< std::endl; std::cout << " SHM/alist.size = " << shm_map_n->size() << std::endl; #endif } catch ( boost::interprocess::bad_alloc e ) { std::cerr << " Out of shared memory..." << std::cerr; std::cout << e.what() <<std::endl; std::cerr << " Shared memory usage: " << segment->get_free_memory() /1024.0/1024.0 << " Mbytes " << std::setprecision ( 2 ) << 100.0- ( 100.0*segment->get_free_memory() ) /segment->get_size() << "% is free" << std::endl; m_cv.notify_one(); m_run = false; m_thread.join(); throw e; } #ifdef DEBUG std::streamsize p = std::cout.precision(); std::cout << " Shared memory usage: " << segment->get_free_memory() /1024.0/1024.0 << " Mbytes " << std::setprecision ( 2 ) << 100.0- ( 100.0*segment->get_free_memory() ) /segment->get_size() << "% is free" << std::setprecision ( p ) << std::endl; #endif shm_map = segment->find<shm_map_Type> ( "JustineMap" ).first; m_cv.notify_one(); }
int main(int argc,char **argv) { char linebuffer[300]; FILE *ifile = fopen("/tmp/refs", "r"); char *t; unsigned int un; struct rc_obj *curr_obj, *count1_obj; struct rc_obj lookup; struct ast_hashtab_iter *it; struct ast_hashtab *objhash; if (!ifile) { printf("Sorry, Cannot open /tmp/refs!\n"); exit(10); } objhash = ast_hashtab_create(9000, hashtab_compare_rc, ast_hashtab_resize_java, ast_hashtab_newsize_java, hashtab_hash_rc, 1); while (fgets(linebuffer, sizeof(linebuffer), ifile)) { /* collect data about the entry */ un = strtoul(linebuffer, &t, 16); lookup.addr = un; lookup.count = 1; count1_obj = ast_hashtab_lookup(objhash, &lookup); if (count1_obj) { /* there IS a count1 obj, so let's see which one we REALLY want */ if (*(t+1) == '=') { /* start a new object! */ curr_obj = alloc_obj(un, ++count1_obj->last_count); /* put it in the hashtable */ ast_hashtab_insert_safe(objhash, curr_obj); } else { if (count1_obj->last_count > 1) { lookup.count = count1_obj->last_count; curr_obj = ast_hashtab_lookup(objhash, &lookup); } else { curr_obj = count1_obj; } } } else { /* NO obj at ALL? -- better make one! */ if (*(t+1) != '=') { printf("BAD: object %x appears without previous allocation marker!\n", un); } curr_obj = count1_obj = alloc_obj(un, 1); /* put it in the hashtable */ ast_hashtab_insert_safe(objhash, curr_obj); } if (*(t+1) == '+' || *(t+1) == '-' ) { curr_obj->total_refcount += strtol(t+1, NULL, 10); } else if (*(t+1) == '*') { curr_obj->destroy_count++; } add_to_hist(linebuffer, curr_obj); } fclose(ifile); /* traverse the objects and check for problems */ it = ast_hashtab_start_traversal(objhash); while ((curr_obj = ast_hashtab_next(it))) { if (curr_obj->total_refcount != 0 || curr_obj->destroy_count != 1) { struct rc_hist *h; if (curr_obj->total_refcount != 0) printf("Problem: net Refcount not zero for object %x\n", curr_obj->addr); if (curr_obj->destroy_count > 1 ) printf("Problem: Object %x destroyed more than once!\n", curr_obj->addr); printf("Object %x history:\n", curr_obj->addr); for(h=curr_obj->hist;h;h=h->next) { printf(" %s", h->desc); } printf("==============\n"); } } ast_hashtab_end_traversal(it); return 0; }
int main() { die_if(SDL_Init(SDL_INIT_VIDEO), "unable to init SDL"); die_if(!(g_window = SDL_CreateWindow(WIN_TITLE, WIN_X, WIN_Y, WIN_WIDTH, WIN_HEIGHT, 0)), "unable to create window"); die_if(!(g_screen = SDL_GetWindowSurface(g_window)), "unable to create window surface"); die_if(!(g_renderer = SDL_CreateRenderer(g_window, -1, SDL_RENDERER_ACCELERATED)), "unable to create renderer"); die_if(SDL_SetRenderDrawColor( g_renderer, 0x0, 0x0, 0x0, 0xFF ), "unable to set draw color"); g_perf_freq = SDL_GetPerformanceFrequency(); // init textures { SDL_Surface *srfc = NULL; for(unsigned int i = 0; i < NUM_TEX; ++i) { die_if(!(srfc = SDL_LoadBMP((char*)g_tex[i])), "unable to load bitmap"); die_if(!(g_tex[i] = SDL_CreateTextureFromSurface(g_renderer, srfc)) , "unable to create bmp texture"); g_tex_w[i] = srfc->w; g_tex_h[i] = srfc->h; SDL_FreeSurface(srfc); } } alloc_obj(SHIP_TID); int nufos = NUM_UFOS; for(int i = 0; i < nufos; ++i) { alloc_obj(UFO_TID); } Uint64 t0 = SDL_GetPerformanceCounter(); /* used to optimize (minimize) calls to rand() */ #define HASBITS(x,y) (((x) & (y)) == (y)) /* int foo = 0; */ /* for(int i = 0; i < 100000; ++i) */ /* { */ /* int r = rand(); */ /* if (HASBITS(r,0x7) || HASBITS(r,0x70)) ++foo; */ /* } */ // printf("%d\n", foo); // exit(-1); while (1) { // SDL_Delay(10); SDL_Event e; Uint64 t1 = SDL_GetPerformanceCounter(); float dt; dt = (float)(t1 - t0)/g_perf_freq; t0 = t1; /* user events */ if (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) { exit_cleanup(0); } if (e.type == SDL_KEYDOWN) { SDL_Keycode sym = e.key.keysym.sym; if(sym == SDLK_SPACE) { alloc_timer_1s(alloc_obj(PHOTON_TID), t1); } g_obj_ax[0] = sym == SDLK_LEFT ? -(X_ACC) : (sym == SDLK_RIGHT ? X_ACC : 0.0f); g_obj_ay[0] = sym == SDLK_UP ? -(Y_ACC) : (sym == SDLK_DOWN ? Y_ACC : 0.0f); } } /* ai events */ for(int i = 1; i < nufos + 1; ++i) { int r = rand(); #define RAND_FREQ 0xf g_obj_ax[i] = HASBITS(r,RAND_FREQ) ? X_ACC : 0.0f; g_obj_ax[i] += HASBITS(r,RAND_FREQ << 8) ? -(X_ACC) : 0.0f; g_obj_ax[i] /= 100.0f; g_obj_ay[i] = HASBITS(r,RAND_FREQ << 16) ? Y_ACC : 0.0f; g_obj_ay[i] += HASBITS(r,RAND_FREQ << 24) ? -(Y_ACC) : 0.0f; g_obj_ay[i] /= 100.0f; } /* timer events */ for(int i = 0; i < g_ntimers; ++i) { if(t1 >= g_timer_expire[i]) { dealloc_timer_1s(i); } } /* physics update */ for(int i = g_nobjs - 1; i >= 0 ; --i) { update(&g_obj_x[i], &g_obj_vx[i], &g_obj_ax[i], dt, -OBJ_W(i), WIN_WIDTH); update(&g_obj_y[i], &g_obj_vy[i], &g_obj_ay[i], dt, -OBJ_H(i), WIN_HEIGHT); } /* collisions */ for(int i = g_nobjs - 1; i >= 0 ; --i) { for(int j = i - 1; j >= 0 ; --j) { if (colliding(i,j)) { } } } /* rendering */ SDL_RenderClear(g_renderer); /* ignore return */ SDL_Texture *tex = g_tex[UFO_TID]; for(int i = 1; i < nufos + 1; ++i) { render(i, tex); } render(0, g_tex[SHIP_TID]); tex = g_tex[PHOTON_TID]; for(int i = 1 + nufos; i < 1 + nufos + g_ntimers; ++i) { render(i, tex); } SDL_RenderPresent(g_renderer); } return 0; }
LISP_OBJ_PTR define(ENVIRONMENT_PTR env, LISP_OBJ_PTR args) { char *var; LISP_OBJ_PTR to_def = car(args); LISP_OBJ_PTR val; LISP_OBJ_PTR fun, params, req_params, opt_params, rest_params; ENVIRONMENT_PTR fun_env; BOOLEAN optional = FALSE, rest = FALSE; switch(form(to_def)) { case SYMBOL_FORM: var = symbol_value(to_def); val = cadr(args); enter_symbol(env, var, val); return to_def; case CONS_FORM: var = symbol_value(car(to_def)); val = cdr(args); params = cdr(to_def); req_params = params; opt_params = nil_ptr; rest_params = nil_ptr; // edge case if (is_pair(params) && !strcmp(symbol_value(car(params)), "&optional")) { req_params = nil_ptr; optional = TRUE; // giving this a try opt_params = cdr(params); cdr(params) = nil_ptr; params = opt_params; } else if (is_pair(params) && !strcmp(symbol_value(car(params)), "&rest")) { req_params = nil_ptr; rest = TRUE; rest_params = cadr(params); } while (params != nil_ptr && !optional && !rest) { if (is_pair(cdr(params)) && !strcmp(symbol_value(cadr(params)), "&optional")) { optional = TRUE; opt_params = cddr(params); cdr(params) = nil_ptr; params = opt_params; break; } else if (is_pair(cdr(params)) && !strcmp(symbol_value(cadr(params)), "&rest")) { rest = TRUE; rest_params = caddr(params); cdr(params) = nil_ptr; params = rest_params; break; } params = cdr(params); } while (optional && params != nil_ptr && !rest) { if (is_pair(cdr(params)) && !strcmp(symbol_value(cadr(params)), "&rest")) { rest = TRUE; rest_params = caddr(params); cdr(params) = nil_ptr; params = rest_params; break; } params = cdr(params); } fun = alloc_obj(); form(fun) = PROCEDURE_FORM; proc_type(fun) = DERIVED; proc_env(fun) = env; proc_reqparams(fun) = req_params; proc_optparams(fun) = opt_params; proc_restparams(fun) = rest_params; proc_body(fun) = val; enter_symbol(env, var, fun); return car(to_def); } }
static void* major_alloc_object (int size, gboolean has_references) { return alloc_obj (size, FALSE, has_references); }
/* size is a multiple of SGEN_ALLOC_ALIGN */ static void* major_alloc_small_pinned_obj (size_t size, gboolean has_references) { return alloc_obj (size, TRUE, has_references); }