int ctf_hash_create(ctf_hash_t *hp, ulong_t nelems) { if (nelems > USHRT_MAX) return (EOVERFLOW); /* * If the hash table is going to be empty, don't bother allocating any * memory and make the only bucket point to a zero so lookups fail. */ if (nelems == 0) { bzero(hp, sizeof (ctf_hash_t)); hp->h_buckets = (ushort_t *)_CTF_EMPTY; hp->h_nbuckets = 1; return (0); } hp->h_nbuckets = 211; /* use a prime number of hash buckets */ hp->h_nelems = nelems + 1; /* we use index zero as a sentinel */ hp->h_free = 1; /* first free element is index 1 */ hp->h_buckets = ctf_alloc(sizeof (ushort_t) * hp->h_nbuckets); hp->h_chains = ctf_alloc(sizeof (ctf_helem_t) * hp->h_nelems); if (hp->h_buckets == NULL || hp->h_chains == NULL) { ctf_hash_destroy(hp); return (EAGAIN); } bzero(hp->h_buckets, sizeof (ushort_t) * hp->h_nbuckets); bzero(hp->h_chains, sizeof (ctf_helem_t) * hp->h_nelems); return (0); }
/* * Close the specified CTF container and free associated data structures. Note * that ctf_close() is a reference counted operation: if the specified file is * the parent of other active containers, its reference count will be greater * than one and it will be freed later when no active children exist. */ void ctf_close(ctf_file_t *fp) { ctf_dtdef_t *dtd, *ntd; ctf_dmdef_t *dmd, *nmd; if (fp == NULL) return; /* allow ctf_close(NULL) to simplify caller code */ ctf_dprintf("ctf_close(%p) refcnt=%u\n", (void *)fp, fp->ctf_refcnt); if (fp->ctf_refcnt > 1) { fp->ctf_refcnt--; return; } for (dtd = ctf_list_next(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) { switch (CTF_INFO_KIND(dtd->dtd_data.ctt_info)) { case CTF_K_STRUCT: case CTF_K_UNION: case CTF_K_ENUM: for (dmd = ctf_list_next(&dtd->dtd_u.dtu_members); dmd != NULL; dmd = nmd) { if (dmd->dmd_name != NULL) { ctf_free(dmd->dmd_name, strlen(dmd->dmd_name) + 1); } nmd = ctf_list_next(dmd); ctf_free(dmd, sizeof (ctf_dmdef_t)); } break; case CTF_K_FUNCTION: ctf_free(dtd->dtd_u.dtu_argv, sizeof (ctf_id_t) * CTF_INFO_VLEN(dtd->dtd_data.ctt_info)); break; } if (dtd->dtd_name != NULL) ctf_free(dtd->dtd_name, strlen(dtd->dtd_name) + 1); ntd = ctf_list_next(dtd); ctf_free(dtd, sizeof (ctf_dtdef_t)); } if (fp->ctf_parent != NULL) ctf_close(fp->ctf_parent); if (fp->ctf_flags & LCTF_MMAP) { if (fp->ctf_data.cts_data != NULL) ctf_sect_munmap(&fp->ctf_data); if (fp->ctf_symtab.cts_data != NULL) ctf_sect_munmap(&fp->ctf_symtab); if (fp->ctf_strtab.cts_data != NULL) ctf_sect_munmap(&fp->ctf_strtab); } if (fp->ctf_data.cts_name != _CTF_NULLSTR && fp->ctf_data.cts_name != NULL) { ctf_free((char *)fp->ctf_data.cts_name, strlen(fp->ctf_data.cts_name) + 1); } if (fp->ctf_symtab.cts_name != _CTF_NULLSTR && fp->ctf_symtab.cts_name != NULL) { ctf_free((char *)fp->ctf_symtab.cts_name, strlen(fp->ctf_symtab.cts_name) + 1); } if (fp->ctf_strtab.cts_name != _CTF_NULLSTR && fp->ctf_strtab.cts_name != NULL) { ctf_free((char *)fp->ctf_strtab.cts_name, strlen(fp->ctf_strtab.cts_name) + 1); } if (fp->ctf_base != fp->ctf_data.cts_data && fp->ctf_base != NULL) ctf_data_free((void *)fp->ctf_base, fp->ctf_size); if (fp->ctf_sxlate != NULL) ctf_free(fp->ctf_sxlate, sizeof (uint_t) * fp->ctf_nsyms); if (fp->ctf_txlate != NULL) { ctf_free(fp->ctf_txlate, sizeof (uint_t) * (fp->ctf_typemax + 1)); } if (fp->ctf_ptrtab != NULL) { ctf_free(fp->ctf_ptrtab, sizeof (ushort_t) * (fp->ctf_typemax + 1)); } ctf_hash_destroy(&fp->ctf_structs); ctf_hash_destroy(&fp->ctf_unions); ctf_hash_destroy(&fp->ctf_enums); ctf_hash_destroy(&fp->ctf_names); ctf_free(fp, sizeof (ctf_file_t)); }
/* * Close the specified CTF container and free associated data structures. Note * that ctf_close() is a reference counted operation: if the specified file is * the parent of other active containers, its reference count will be greater * than one and it will be freed later when no active children exist. */ void ctf_close(ctf_file_t *fp) { ctf_dtdef_t *dtd, *ntd; if (fp == NULL) return; /* allow ctf_close(NULL) to simplify caller code */ ctf_dprintf("ctf_close(%p) refcnt=%u\n", (void *)fp, fp->ctf_refcnt); if (fp->ctf_refcnt > 1) { fp->ctf_refcnt--; return; } if (fp->ctf_parent != NULL) ctf_close(fp->ctf_parent); for (dtd = ctf_list_next(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) { ntd = ctf_list_next(dtd); ctf_dtd_delete(fp, dtd); } ctf_free(fp->ctf_dthash, fp->ctf_dthashlen * sizeof (ctf_dtdef_t *)); if (fp->ctf_flags & LCTF_MMAP) { if (fp->ctf_data.cts_data != NULL) ctf_sect_munmap(&fp->ctf_data); if (fp->ctf_symtab.cts_data != NULL) ctf_sect_munmap(&fp->ctf_symtab); if (fp->ctf_strtab.cts_data != NULL) ctf_sect_munmap(&fp->ctf_strtab); } if (fp->ctf_data.cts_name != _CTF_NULLSTR && fp->ctf_data.cts_name != NULL) { ctf_free((char *)fp->ctf_data.cts_name, strlen(fp->ctf_data.cts_name) + 1); } if (fp->ctf_symtab.cts_name != _CTF_NULLSTR && fp->ctf_symtab.cts_name != NULL) { ctf_free((char *)fp->ctf_symtab.cts_name, strlen(fp->ctf_symtab.cts_name) + 1); } if (fp->ctf_strtab.cts_name != _CTF_NULLSTR && fp->ctf_strtab.cts_name != NULL) { ctf_free((char *)fp->ctf_strtab.cts_name, strlen(fp->ctf_strtab.cts_name) + 1); } if (fp->ctf_base != fp->ctf_data.cts_data && fp->ctf_base != NULL) ctf_data_free((void *)fp->ctf_base, fp->ctf_size); if (fp->ctf_sxlate != NULL) ctf_free(fp->ctf_sxlate, sizeof (uint_t) * fp->ctf_nsyms); if (fp->ctf_txlate != NULL) { ctf_free(fp->ctf_txlate, sizeof (uint_t) * (fp->ctf_typemax + 1)); } if (fp->ctf_ptrtab != NULL) { ctf_free(fp->ctf_ptrtab, sizeof (ushort_t) * (fp->ctf_typemax + 1)); } ctf_hash_destroy(&fp->ctf_structs); ctf_hash_destroy(&fp->ctf_unions); ctf_hash_destroy(&fp->ctf_enums); ctf_hash_destroy(&fp->ctf_names); ctf_free(fp, sizeof (ctf_file_t)); }
/* * Close the specified CTF container and free associated data structures. Note * that ctf_close() is a reference counted operation: if the specified file is * the parent of other active containers, its reference count will be greater * than one and it will be freed later when no active children exist. */ void ctf_close(ctf_file_t *fp) { ctf_dtdef_t *dtd, *ntd; if (fp == NULL) return; /* allow ctf_close(NULL) to simplify caller code */ ctf_dprintf("ctf_close(%p) refcnt=%u\n", (void *)fp, fp->ctf_refcnt); if (fp->ctf_refcnt > 1) { fp->ctf_refcnt--; return; } if (fp->ctf_parent != NULL) ctf_close(fp->ctf_parent); /* * Note, to work properly with reference counting on the dynamic * section, we must delete the list in reverse. */ for (dtd = ctf_list_prev(&fp->ctf_dtdefs); dtd != NULL; dtd = ntd) { ntd = ctf_list_prev(dtd); ctf_dtd_delete(fp, dtd); } ctf_free(fp->ctf_dthash, fp->ctf_dthashlen * sizeof (ctf_dtdef_t *)); if (fp->ctf_flags & LCTF_MMAP) { if (fp->ctf_data.cts_data != NULL) ctf_sect_munmap(&fp->ctf_data); if (fp->ctf_symtab.cts_data != NULL) ctf_sect_munmap(&fp->ctf_symtab); if (fp->ctf_strtab.cts_data != NULL) ctf_sect_munmap(&fp->ctf_strtab); } if (fp->ctf_data.cts_name != _CTF_NULLSTR && fp->ctf_data.cts_name != NULL) { ctf_free(__UNCONST(fp->ctf_data.cts_name), strlen(fp->ctf_data.cts_name) + 1); } if (fp->ctf_symtab.cts_name != _CTF_NULLSTR && fp->ctf_symtab.cts_name != NULL) { ctf_free(__UNCONST(fp->ctf_symtab.cts_name), strlen(fp->ctf_symtab.cts_name) + 1); } if (fp->ctf_strtab.cts_name != _CTF_NULLSTR && fp->ctf_strtab.cts_name != NULL) { ctf_free(__UNCONST(fp->ctf_strtab.cts_name), strlen(fp->ctf_strtab.cts_name) + 1); } if (fp->ctf_base != fp->ctf_data.cts_data && fp->ctf_base != NULL) ctf_data_free(__UNCONST(fp->ctf_base), fp->ctf_size); if (fp->ctf_sxlate != NULL) ctf_free(fp->ctf_sxlate, sizeof (uint_t) * fp->ctf_nsyms); if (fp->ctf_txlate != NULL) { ctf_free(fp->ctf_txlate, sizeof (uint_t) * (fp->ctf_typemax + 1)); } if (fp->ctf_ptrtab != NULL) { ctf_free(fp->ctf_ptrtab, sizeof (ushort_t) * (fp->ctf_typemax + 1)); } ctf_hash_destroy(&fp->ctf_structs); ctf_hash_destroy(&fp->ctf_unions); ctf_hash_destroy(&fp->ctf_enums); ctf_hash_destroy(&fp->ctf_names); ctf_free(fp, sizeof (ctf_file_t)); }