void rvmRegisterReference(Env* env, Object* reference, Object* referent) { if (referent) { // Add 'reference' to the references list for 'referent' in the referents hashtable rvmLockMutex(&referentsLock); ReferenceList* l = rvmAllocateMemory(env, sizeof(ReferenceList)); if (!l) goto done; // OOM thrown l->reference = reference; void* key = (void*) GC_HIDE_POINTER(referent); // Hide the pointer from the GC so that it doesn't prevent the referent from being GCed. ReferentEntry* referentEntry; HASH_FIND_PTR(referents, &key, referentEntry); if (!referentEntry) { // referent is not in the hashtable. Add it. referentEntry = rvmAllocateMemory(env, sizeof(ReferentEntry)); if (!referentEntry) goto done; // OOM thrown referentEntry->key = key; HASH_ADD_PTR(referents, key, referentEntry); } // Add the reference to the referent's list of references LL_PREPEND(referentEntry->references, l); // Register the referent for finalization GC_REGISTER_FINALIZER_NO_ORDER(referent, _finalizeObject, NULL, NULL, NULL); done: rvmUnlockMutex(&referentsLock); } }
/****************************************************************************** tv_material_compile_shader Compiles the given shader (first looking to see if the shader already has been loaded by checking the appropriate "loaded_XXX_shaders" table. *****************************************************************************/ GLuint tv_material_compile_shader(tvchar* file, tvuint type) { TvMaterialShader* lup; tvchar* buffer; TvMaterialShader* shader_table; GLuint shader_handle; /* look up the shader - has it been loaded already? */ HASH_FIND_PTR(loaded_vertex_shaders, file, lup); switch(type) { case GL_VERTEX_SHADER: shader_table = loaded_fragment_shaders; break; case GL_FRAGMENT_SHADER: shader_table = loaded_vertex_shaders; break; case GL_GEOMETRY_SHADER: shader_table = loaded_geometry_shaders; break; case GL_TESS_CONTROL_SHADER: shader_table = loaded_tesselation_control_shaders; break; case GL_TESS_EVALUATION_SHADER: shader_table = loaded_tesselation_evaluation_shaders; break; default: tv_warning("unrecognized shader type"); return; } /* look up the shader */ HASH_FIND_PTR(shader_table, file, lup); /* if the shader has already been loaded, return it's handle. */ if(lup) { return (GLuint)lup->id; } /* shader has NOT been loaded, let's load it */ UtilReadFile(file, &buffer); shader_handle = compile_gl_shader(buffer, type); lup = (TvMaterialShader*)tv_alloc(sizeof(TvMaterialShader)); lup->name = file; lup->id = shader_handle; HASH_ADD_PTR(shader_table, name, lup); free(buffer); return (GLuint)shader_handle; }
void ActionManager::addAction(Action *action, Node *target, bool paused) { CCASSERT(action != NULL, ""); CCASSERT(target != NULL, ""); tHashElement *element = NULL; // we should convert it to Object*, because we save it as Object* Object *tmp = target; HASH_FIND_PTR(m_pTargets, &tmp, element); if (! element) { element = (tHashElement*)kdCalloc(sizeof(*element), 1); element->paused = paused; target->retain(); element->target = target; HASH_ADD_PTR(m_pTargets, target, element); } actionAllocWithHashElement(element); CCASSERT(! ccArrayContainsObject(element->actions, action), ""); ccArrayAppendObject(element->actions, action); action->startWithTarget(target); }
void ActionManager::addAction(Action *action, Node *target, bool paused) { CCASSERT(action != nullptr, "action can't be nullptr!"); CCASSERT(target != nullptr, "target can't be nullptr!"); if(action == nullptr || target == nullptr) return; tHashElement *element = nullptr; // we should convert it to Ref*, because we save it as Ref* Ref *tmp = target; HASH_FIND_PTR(_targets, &tmp, element); if (! element) { element = (tHashElement*)calloc(sizeof(*element), 1); element->paused = paused; target->retain(); element->target = target; HASH_ADD_PTR(_targets, target, element); } actionAllocWithHashElement(element); CCASSERT(! ccArrayContainsObject(element->actions, action), "action already be added!"); ccArrayAppendObject(element->actions, action); action->startWithTarget(target); }
void debug_alloc_add (void *a, size_t s) { alloc_table_t *at = debug_alloc_new (a, s); HASH_ADD_PTR (atp, addr, at); }
jboolean buildLoadedClassesHash(Env* env, Class* clazz, void* data) { LoadedClass** hashPtr = (LoadedClass**) data; LoadedClass* entry = calloc(1, sizeof(LoadedClass)); entry->key = clazz; HASH_ADD_PTR(*hashPtr, key, entry); return TRUE; }
static int heapStatsCallback(void* ptr, unsigned char kind, size_t sz, int live, void* _data) { HeapStatsCallbackData* data = (HeapStatsCallbackData*) _data; LoadedClass* loadedClasses = data->loadedClasses; HeapStat** statsHashPtr = data->statsHashPtr; Class* key = NULL; if (kind == GC_gcj_kind || kind == objectArrayGCKind) { Object* obj = (Object*) ptr; if (obj && obj->clazz) { LoadedClass* loadedClass; HASH_FIND_PTR(loadedClasses, &(obj->clazz), loadedClass); if (!loadedClass) { return 0; // Don't clear this object } key = obj->clazz; } } HeapStat* stat; HASH_FIND_PTR(*statsHashPtr, &key, stat); if (!stat) { stat = calloc(1, sizeof(HeapStat)); stat->key = key; HASH_ADD_PTR(*statsHashPtr, key, stat); } stat->numberOfInstances++; stat->numberOfBytes += sz; if (live) { stat->numberOfLiveInstances++; stat->numberOfLiveBytes += sz; } return 0; // live ? 0 : 1; }
/* Migrate pixmap to UMP buffer */ static UMPBufferInfoPtr MigratePixmapToUMP(PixmapPtr pPixmap) { ScreenPtr pScreen = pPixmap->drawable.pScreen; ScrnInfoPtr pScrn = xf86Screens[pScreen->myNum]; Rk30MaliPtr rk_3d = FBDEVPTR(pScrn)->Rk30Mali; UMPBufferInfoPtr umpbuf; size_t pitch = ((pPixmap->devKind + 7) / 8) * 8; size_t size = pitch * pPixmap->drawable.height; HASH_FIND_PTR(rk_3d->HashPixmapToUMP, &pPixmap, umpbuf); if (umpbuf) { DebugMsg("MigratePixmapToUMP %p, already exists = %p\n", pPixmap, umpbuf); return umpbuf; } /* create the UMP buffer */ umpbuf = calloc(1, sizeof(UMPBufferInfoRec)); if (!umpbuf) { ErrorF("MigratePixmapToUMP: calloc failed\n"); return NULL; } umpbuf->refcount = 1; umpbuf->pPixmap = pPixmap; umpbuf->handle = ump_ref_drv_allocate(size, UMP_REF_DRV_CONSTRAINT_PHYSICALLY_LINEAR); if (umpbuf->handle == UMP_INVALID_MEMORY_HANDLE) { ErrorF("MigratePixmapToUMP: ump_ref_drv_allocate failed\n"); free(umpbuf); return NULL; } umpbuf->size = size; umpbuf->addr = ump_mapped_pointer_get(umpbuf->handle); umpbuf->depth = pPixmap->drawable.depth; umpbuf->width = pPixmap->drawable.width; umpbuf->height = pPixmap->drawable.height; /* copy the pixel data to the new location */ if (pitch == pPixmap->devKind) { memcpy(umpbuf->addr, pPixmap->devPrivate.ptr, size); } else { int y; for (y = 0; y < umpbuf->height; y++) { memcpy(umpbuf->addr + y * pitch, pPixmap->devPrivate.ptr + y * pPixmap->devKind, pPixmap->devKind); } } umpbuf->BackupDevKind = pPixmap->devKind; umpbuf->BackupDevPrivatePtr = pPixmap->devPrivate.ptr; pPixmap->devKind = pitch; pPixmap->devPrivate.ptr = umpbuf->addr; HASH_ADD_PTR(rk_3d->HashPixmapToUMP, pPixmap, umpbuf); DebugMsg("MigratePixmapToUMP %p, new buf = %p\n", pPixmap, umpbuf); return umpbuf; }
/* * Allocate memory. * * size Number of bytes to allocate. * descr Short description of what this memory will be used for. */ void * mem_alloc(uint size, const char *descr) { UNUSED(descr); assert(size > 0 && descr); void *ptr = malloc(size); #if MEMDEBUG if (alloc_mutex == NULL) /* XXX Race condition.. */ alloc_mutex = SDL_CreateMutex(); SDL_mutexP(alloc_mutex); { assert(ptr); allocation *alc = malloc(sizeof(allocation)); alc->ptr = ptr; alc->size = size; snprintf(alc->descr, sizeof(alc->descr), "%s", descr); HASH_ADD_PTR(alloc_hash, ptr, alc); /* Update total number of allocated bytes. */ total_bytes += size; log_msg("[MEM] Alloc %p=%s for `%s` (total: %s)", ptr, human(size), descr, human(total_bytes)); } SDL_mutexV(alloc_mutex); #endif return ptr; }
void save_size(void *ptr, size_t size) { hash_t *elem; elem = (hash_t*) malloc(sizeof(hash_t)); elem -> key = ptr; elem -> size = size; if(get_verbose_level() >= DEBUG) printf("Storing (%p,%ld)\n",ptr,size); HASH_ADD_PTR( size_hash, key, elem ); }
void jsb_set_jsobject_for_proxy(JSObject *jsobj, void* proxy) { CCASSERT( !jsb_get_jsobject_for_proxy(proxy), "Already added. abort"); tHashJSObject *element = (tHashJSObject*) malloc( sizeof( *element ) ); element->proxy = proxy; element->jsObject = jsobj; HASH_ADD_PTR( reverse_hash, proxy, element ); }
void save_ptr(void *ptr, size_t size, char *file, int line) { hash_t *elem; elem = (hash_t*) malloc(sizeof(hash_t)); elem -> key = ptr; elem -> size = size; elem -> line = line; elem -> file = my_strdup(file); if(tm_get_verbose_level() >= DEBUG) printf("Storing (%p,%ld)\n",ptr,size); HASH_ADD_PTR( size_hash, key, elem ); }
/** * Returns the ReferentEntry for the specified object or creates one and adds * it to the referents hash if none exists. referentsLock MUST be held. */ static ReferentEntry* getReferentEntryForObject(Env* env, Object* o) { void* key = (void*) GC_HIDE_POINTER(o); // Hide the pointer from the GC so that the key doesn't prevent the object from being GCed. ReferentEntry* referentEntry; HASH_FIND_PTR(referents, &key, referentEntry); if (!referentEntry) { // Object is not in the hashtable. Add it. referentEntry = allocateMemoryOfKind(env, sizeof(ReferentEntry), referentEntryGCKind); if (!referentEntry) return NULL; // OOM thrown referentEntry->key = key; HASH_ADD_PTR(referents, key, referentEntry); } return referentEntry; }
void jsb_set_proxy_for_jsobject(void *proxy, JSObject *obj) { CCASSERT( !jsb_get_proxy_for_jsobject(obj), "Already added. abort"); // printf("Setting proxy for: %p - %p (%s)\n", obj, proxy, [[proxy description] UTF8String] ); tHashJSObject *element = (tHashJSObject*) malloc( sizeof( *element ) ); // XXX: Do not retain it here. // [proxy retain]; element->proxy = proxy; element->jsObject = obj; HASH_ADD_PTR( hash, jsObject, element ); }
graph* depthFirstClone(graph *node, HashMap **map) { HashMap *found, *elem; graph *key = node, *copy; HASH_FIND_PTR(*map, &key, found); if (found) { return found->copy; } elem = malloc(sizeof(HashMap)); copy = (graph*)malloc(sizeof(graph)); memset(copy, 0, sizeof(graph)); copy->label = node->label; copy->neighborsCount = node->neighborsCount; elem->node = node; elem->copy = copy; HASH_ADD_PTR(*map, node, elem); for (int i = 0; i < NEIGHBORS_MAX_SIZE; i++) { if (node->neighbors[i] != NULL) { copy->neighbors[i] = depthFirstClone(node->neighbors[i], map); } } return copy; }
/* * Reallocate memory. * * ptr Address of a pointer to the resizable memory. * size Number of bytes to allocate. * descr Short description of what this memory will be used for. */ void mem_realloc(void **ptr, uint size, const char *descr) { assert(ptr && descr); /* If *ptr is NULL, then do mem_alloc(). */ if (*ptr == NULL) { *ptr = mem_alloc(size, descr); return; } *ptr = realloc(*ptr, size); #if MEMDEBUG SDL_mutexP(alloc_mutex); { /* Find previous allocation. */ assert(*ptr); allocation *alc; HASH_FIND_PTR(alloc_hash, ptr, alc); assert(alc != NULL); /* Update total number of allocated bytes. */ total_bytes += (size - alc->size); log_msg("[MEM] Realloc %p=%s -> %p=%s for `%s` (total: %s)", alc->ptr, human(alc->size), *ptr, human(size), descr, human(total_bytes)); /* Remove from hash and update allocation data. */ HASH_DEL(alloc_hash, alc); alc->ptr = *ptr; alc->size = size; assert(strcmp(descr, alc->descr) == 0); /* Re-add to hash. */ HASH_ADD_PTR(alloc_hash, ptr, alc); } SDL_mutexV(alloc_mutex); #endif }
int main() { el_t *d; el_t *hash = NULL; char *someaddr = NULL; el_t *e = (el_t*)malloc(sizeof(el_t)); if (!e) { return -1; } e->key = (void*)someaddr; e->i = 1; HASH_ADD_PTR(hash,key,e); HASH_FIND_PTR(hash, &someaddr, d); if (d != NULL) { printf("found\n"); } /* release memory */ HASH_DEL(hash,e); free(e); return 0; }
static void account_alloc(void *ptr, size_t size) { // Do not account itself no_hook = 1; // Allocating memory if (size != 0) { struct malloc_item *item, *out; item = malloc(sizeof(*item)); item->p = ptr; item->size = size; HASH_ADD_PTR(HT, p, item); mem_allocated += size; log("Alloc: %p -> %zu\n", ptr, size); } // Freeing memory else { struct malloc_item *found; HASH_FIND_PTR(HT, &ptr, found); if (found) { mem_allocated -= found->size; log("Free: %p -> %zu\n", found->p, found->size); HASH_DEL(HT, found); free(found); } else { log("Freeing unaccounted allocation %p\n", ptr); } } log(" [[[::: %d (%u) :::]]] \n", mem_allocated, HASH_COUNT(HT)); no_hook = 0; }
void *xpl_alloc(size_t size) { void *ptr = malloc(size); if (ptr == NULL) { LOG_ERROR("malloc failed for %lu bytes", (unsigned long)size); abort(); } xpl_allocation_t *allocation_info = (xpl_allocation_t *)malloc(sizeof(xpl_allocation_t)); if (allocation_info == NULL) { LOG_ERROR("malloc failed for table entry of %lu bytes", (unsigned long)sizeof(xpl_allocation_t)); abort(); } allocation_info->handle = ptr; allocation_info->bytes = size; HASH_ADD_PTR(allocations, handle, allocation_info); LOG_TRACE("+ %p %lu bytes entry: %p", allocation_info->handle, allocation_info->bytes, allocation_info); allocated_bytes += size; return ptr; }
/* * scan the ELF image to build table of global symbold and the image * regions where they can be found (BSS and DATA) */ static inline int table_init_helper (void) { Elf *e = NULL; GElf_Ehdr ehdr; char *shstr_name = NULL; size_t shstrndx; Elf_Scn *scn = NULL; GElf_Shdr shdr; int ret = -1; #if 0 int (*getsi) (); /* look up name of elf_get... routine */ #endif /* unrecognized format */ if (elf_version (EV_CURRENT) == EV_NONE) { goto bail; } /* get the ELF object from already opened state */ e = elf_begin (GET_STATE (exe_fd), ELF_C_READ, NULL); if (e == NULL) { goto bail; } /* do some sanity checks */ if (elf_kind (e) != ELF_K_ELF) { goto bail; } if (gelf_getehdr (e, &ehdr) == NULL) { goto bail; } if (gelf_getclass (e) == ELFCLASSNONE) { goto bail; } /* * There are various elf_get* routines with different return values * in differnt libraries/versions - name of routine does not tell us * all we ned to know. So let it go here, and we'll mop up any * problems later on. */ /* * This routine is either "elf_getshdrstrndx" in newer ELF * libraries, or "elf_getshstrndx" in older ones. Hard-code older * one for now since it is in the newer libraries, although marked as * deprecated. This will be detected by autoconf later * * DEPRECATED */ (void) elf_getshstrndx (e, &shstrndx); /* walk sections, look for RO/BSS/DATA and symbol table */ scn = NULL; while ((scn = elf_nextscn (e, scn)) != NULL) { if (gelf_getshdr (scn, &shdr) != &shdr) { goto bail; } shstr_name = elf_strptr (e, shstrndx, shdr.sh_name); if (shstr_name == NULL) { goto bail; } /* found the read-only data */ if (shdr.sh_type == SHT_PROGBITS && strcmp (shstr_name, ".rodata") == 0) { elfro.start = shdr.sh_addr; elfro.end = elfro.start + shdr.sh_size; shmemi_trace (SHMEM_LOG_SYMBOLS, "ELF section .rodata for global variables = 0x%lX -> 0x%lX", elfro.start, elfro.end); continue; /* move to next scan */ } /* found the uninitialized globals */ if (shdr.sh_type == SHT_NOBITS && strcmp (shstr_name, ".bss") == 0) { elfbss.start = shdr.sh_addr; elfbss.end = elfbss.start + shdr.sh_size; shmemi_trace (SHMEM_LOG_SYMBOLS, "ELF section .bss for global variables = 0x%lX -> 0x%lX", elfbss.start, elfbss.end); continue; /* move to next scan */ } /* found the initialized globals */ if (shdr.sh_type == SHT_PROGBITS && strcmp (shstr_name, ".data") == 0) { elfdata.start = shdr.sh_addr; elfdata.end = elfdata.start + shdr.sh_size; shmemi_trace (SHMEM_LOG_SYMBOLS, "ELF section .data for global variables = 0x%lX -> 0x%lX", elfdata.start, elfdata.end); continue; /* move to next scan */ } /* keep looking until we find the symbol table */ if (shdr.sh_type == SHT_SYMTAB) { Elf_Data *data = NULL; while ((data = elf_getdata (scn, data)) != NULL) { GElf_Sym *es; GElf_Sym *last_es; es = (GElf_Sym *) data->d_buf; if (es == NULL) { continue; } /* find out how many entries to look for */ last_es = (GElf_Sym *) ((char *) data->d_buf + data->d_size); for (; es < last_es; es += 1) { char *name; /* * need visible global or local (Fortran save) object with * some kind of content */ if (es->st_value == 0 || es->st_size == 0) { continue; } /* * this macro handles a symbol that is present * in one libelf implementation but isn't in another * (elfutils vs. libelf) */ #ifndef GELF_ST_VISIBILITY #define GELF_ST_VISIBILITY(o) ELF64_ST_VISIBILITY(o) #endif if (GELF_ST_TYPE (es->st_info) != STT_OBJECT && GELF_ST_VISIBILITY (es->st_info) != STV_DEFAULT) { continue; } name = elf_strptr (e, shdr.sh_link, (size_t) es->st_name); if (name == NULL || *name == '\0') { continue; } /* put the symbol and info into the symbol hash table */ { globalvar_t *gv = (globalvar_t *) malloc (sizeof (*gv)); if (gv == NULL) { goto bail; } gv->name = strdup (name); if (gv->name == NULL) { free (gv); goto bail; } gv->addr = (void *) es->st_value; gv->size = es->st_size; HASH_ADD_PTR (gvp, addr, gv); } } } /* * pulled out all the global symbols => success, * don't need to scan further */ ret = 0; break; } } bail: if (elf_end (e) != 0) { ret = -1; } return ret; }
void tv_material_load(tv_material *mat, const char* file) { tvchar* text; cJSON* root = NULL; cJSON* json = NULL; tv_material *lup_mat = NULL; /* check if the material is already loaded, if it is copy it and return. */ HASH_FIND_PTR(loaded_materials, file, lup_mat); if(lup_mat) { /* TOOD: will only work if materials are never deleted */ mat = lup_mat; return; } /* if no material was given, create a new material */ if(mat == NULL) { mat = tv_material_new(); } mat->num_passes = 0; /* material is not loaded, load file and parse it */ UtilReadFile(file, &text); root = cJSON_Parse(text); /* check for errors, return NULL if encountered */ if(!root) { fprintf(stderr, "Error: JSON parse error before: [%s]\n", cJSON_GetErrorPtr()); return; } root = root->child; /* make sure "material" object exists */ if(strncmp(root->string, "material", 8) != 0) { tv_warning("unrecognized JSON object name %s" " for material\n", root->string); } else { /* read the material file */ root = root->child; while(root != NULL) { if(strncmp(root->string, "passes", sizeof("passes")) == 0) { /* read all the material's passes */ for(json = root->child; json != NULL; json = json->next) { tv_material_pass* pass = tv_material_load_pass(json->child); tv_material_add_pass(mat, pass); } } /* unrecognized node - print warning */ else { tv_warning("Unrecognized JSON object name %s " "for material\n", root->string); } /* move to the next JSON object */ root = root->next; } /* add the material to the table of loaded materials to prevent reloading */ mat->name = (tvchar*)file; HASH_ADD_PTR(loaded_materials, name, mat); } /* cleanup */ free(text); cJSON_Delete(root); cJSON_Delete(json); }