CSSM_RETURN ffutil_CopyData(CSSM_DATA_PTR pDestination, DAL_CONST_DATA_PTR pSource) { VERIFY_PTR(pDestination); VERIFY_PTR(pSource); if (pDestination->Data) { BioAPI_free(pDestination->Data, NULL); pDestination->Data = NULL; } /* Destination->Data is now NULL */ pDestination->Length = pSource->Length; if (!pDestination->Length) return CSSM_OK; pDestination->Data = (uint8*)BioAPI_calloc(pDestination->Length, 1, NULL); if (!pDestination->Data) { pDestination->Length = 0; return CSSMERR_DL_MEMORY_ERROR; } memcpy(pDestination->Data, pSource->Data, pDestination->Length); return CSSM_OK; }
CSSM_RETURN ffutil_CopyDataToApp( CSSM_DL_HANDLE DLHandle, CSSM_DATA_PTR pDestination, DAL_CONST_DATA_PTR pSource) { VERIFY_PTR(pDestination); VERIFY_PTR(pSource); pDestination->Data = NULL; pDestination->Length = pSource->Length; if (!pDestination->Length) return CSSM_OK; pDestination->Data = (uint8 *)App_Calloc(DLHandle, pDestination->Length, 1); if (!pDestination->Data) { pDestination->Length = 0; return CSSMERR_DL_MEMORY_ERROR; } memcpy(pDestination->Data, pSource->Data, pDestination->Length); return CSSM_OK; }
// force the freeing of a piece of memory // TODO: freeing here does not call finaliser void gc_free(void *ptr) { if (MP_STATE_MEM(gc_lock_depth) > 0) { // TODO how to deal with this error? return; } DEBUG_printf("gc_free(%p)\n", ptr); if (VERIFY_PTR(ptr)) { size_t block = BLOCK_FROM_PTR(ptr); if (ATB_GET_KIND(block) == AT_HEAD) { #if MICROPY_ENABLE_FINALISER FTB_CLEAR(block); #endif // set the last_free pointer to this block if it's earlier in the heap if (block / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) { MP_STATE_MEM(gc_last_free_atb_index) = block / BLOCKS_PER_ATB; } // free head and all of its tail blocks do { ATB_ANY_TO_FREE(block); block += 1; } while (ATB_GET_KIND(block) == AT_TAIL); #if EXTENSIVE_HEAP_PROFILING gc_dump_alloc_table(); #endif } else { assert(!"bad free"); } } else if (ptr != NULL) { assert(!"bad free"); } }
// force the freeing of a piece of memory void gc_free(void *ptr_in) { if (gc_lock_depth > 0) { // TODO how to deal with this error? return; } mp_uint_t ptr = (mp_uint_t)ptr_in; DEBUG_printf("gc_free(%p)\n", ptr); if (VERIFY_PTR(ptr)) { mp_uint_t block = BLOCK_FROM_PTR(ptr); if (ATB_GET_KIND(block) == AT_HEAD) { // set the last_free pointer to this block if it's earlier in the heap if (block / BLOCKS_PER_ATB < gc_last_free_atb_index) { gc_last_free_atb_index = block / BLOCKS_PER_ATB; } // free head and all of its tail blocks do { ATB_ANY_TO_FREE(block); block += 1; } while (ATB_GET_KIND(block) == AT_TAIL); #if EXTENSIVE_HEAP_PROFILING gc_dump_alloc_table(); #endif } } }
// force the freeing of a piece of memory void gc_free(void *ptr_in) { machine_uint_t ptr = (machine_uint_t)ptr_in; if (VERIFY_PTR(ptr)) { machine_uint_t block = BLOCK_FROM_PTR(ptr); if (ATB_GET_KIND(block) == AT_HEAD) { // free head and all of its tail blocks do { ATB_ANY_TO_FREE(block); block += 1; } while (ATB_GET_KIND(block) == AT_TAIL); } } }
void gc_collect_root(void **ptrs, size_t len) { for (size_t i = 0; i < len; i++) { void *ptr = ptrs[i]; if (VERIFY_PTR(ptr)) { size_t block = BLOCK_FROM_PTR(ptr); if (ATB_GET_KIND(block) == AT_HEAD) { // An unmarked head: mark it, and mark all its children TRACE_MARK(block, ptr); ATB_HEAD_TO_MARK(block); gc_mark_subtree(block); } } } }
size_t gc_nbytes(const void *ptr) { if (VERIFY_PTR(ptr)) { size_t block = BLOCK_FROM_PTR(ptr); if (ATB_GET_KIND(block) == AT_HEAD) { // work out number of consecutive blocks in the chain starting with this on size_t n_blocks = 0; do { n_blocks += 1; } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL); return n_blocks * BYTES_PER_BLOCK; } } // invalid pointer return 0; }
// force the freeing of a piece of memory void gc_free(void *ptr_in) { if (gc_lock_depth > 0) { // TODO how to deal with this error? return; } machine_uint_t ptr = (machine_uint_t)ptr_in; if (VERIFY_PTR(ptr)) { machine_uint_t block = BLOCK_FROM_PTR(ptr); if (ATB_GET_KIND(block) == AT_HEAD) { // free head and all of its tail blocks do { ATB_ANY_TO_FREE(block); block += 1; } while (ATB_GET_KIND(block) == AT_TAIL); } } }
// Take the given block as the topmost block on the stack. Check all it's // children: mark the unmarked child blocks and put those newly marked // blocks on the stack. When all children have been checked, pop off the // topmost block on the stack and repeat with that one. STATIC void gc_mark_subtree(size_t block) { // Start with the block passed in the argument. size_t sp = 0; for (;;) { // work out number of consecutive blocks in the chain starting with this one size_t n_blocks = 0; do { n_blocks += 1; } while (ATB_GET_KIND(block + n_blocks) == AT_TAIL); // check this block's children void **ptrs = (void**)PTR_FROM_BLOCK(block); for (size_t i = n_blocks * BYTES_PER_BLOCK / sizeof(void*); i > 0; i--, ptrs++) { void *ptr = *ptrs; if (VERIFY_PTR(ptr)) { // Mark and push this pointer size_t childblock = BLOCK_FROM_PTR(ptr); if (ATB_GET_KIND(childblock) == AT_HEAD) { // an unmarked head, mark it, and push it on gc stack TRACE_MARK(childblock, ptr); ATB_HEAD_TO_MARK(childblock); if (sp < MICROPY_ALLOC_GC_STACK_SIZE) { MP_STATE_MEM(gc_stack)[sp++] = childblock; } else { MP_STATE_MEM(gc_stack_overflow) = 1; } } } } // Are there any blocks on the stack? if (sp == 0) { break; // No, stack is empty, we're done. } // pop the next block off the stack block = MP_STATE_MEM(gc_stack)[--sp]; } }
void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) { if (gc_lock_depth > 0) { return NULL; } // check for pure allocation if (ptr_in == NULL) { return gc_alloc(n_bytes, false); } machine_uint_t ptr = (machine_uint_t)ptr_in; // sanity check the ptr if (!VERIFY_PTR(ptr)) { return NULL; } // get first block machine_uint_t block = BLOCK_FROM_PTR(ptr); // sanity check the ptr is pointing to the head of a block if (ATB_GET_KIND(block) != AT_HEAD) { return NULL; } // compute number of new blocks that are requested machine_uint_t new_blocks = (n_bytes + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK; // get the number of consecutive tail blocks and // the number of free blocks after last tail block // stop if we reach (or are at) end of heap machine_uint_t n_free = 0; machine_uint_t n_blocks = 1; // counting HEAD block machine_uint_t max_block = gc_alloc_table_byte_len * BLOCKS_PER_ATB; while (block + n_blocks + n_free < max_block) { if (n_blocks + n_free >= new_blocks) { // stop as soon as we find enough blocks for n_bytes break; } byte block_type = ATB_GET_KIND(block + n_blocks + n_free); switch (block_type) { case AT_FREE: n_free++; continue; case AT_TAIL: n_blocks++; continue; case AT_MARK: assert(0); } break; } // return original ptr if it already has the requested number of blocks if (new_blocks == n_blocks) { return ptr_in; } // check if we can shrink the allocated area if (new_blocks < n_blocks) { // free unneeded tail blocks for (machine_uint_t bl = block + new_blocks; ATB_GET_KIND(bl) == AT_TAIL; bl++) { ATB_ANY_TO_FREE(bl); } return ptr_in; } // check if we can expand in place if (new_blocks <= n_blocks + n_free) { // mark few more blocks as used tail for (machine_uint_t bl = block + n_blocks; bl < block + new_blocks; bl++) { assert(ATB_GET_KIND(bl) == AT_FREE); ATB_FREE_TO_TAIL(bl); } // zero out the additional bytes of the newly allocated blocks (see comment above in gc_alloc) memset((byte*)ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes); return ptr_in; } // can't resize inplace; try to find a new contiguous chain void *ptr_out = gc_alloc(n_bytes, #if MICROPY_ENABLE_FINALISER FTB_GET(block) #else false #endif ); // check that the alloc succeeded if (ptr_out == NULL) { return NULL; } DEBUG_printf("gc_realloc(%p -> %p)\n", ptr_in, ptr_out); memcpy(ptr_out, ptr_in, n_blocks * BYTES_PER_BLOCK); gc_free(ptr_in); return ptr_out; }
void *gc_realloc(void *ptr_in, size_t n_bytes, bool allow_move) { if (MP_STATE_MEM(gc_lock_depth) > 0) { return NULL; } // check for pure allocation if (ptr_in == NULL) { return gc_alloc(n_bytes, false); } // check for pure free if (n_bytes == 0) { gc_free(ptr_in); return NULL; } void *ptr = ptr_in; // sanity check the ptr if (!VERIFY_PTR(ptr)) { return NULL; } // get first block size_t block = BLOCK_FROM_PTR(ptr); // sanity check the ptr is pointing to the head of a block if (ATB_GET_KIND(block) != AT_HEAD) { return NULL; } // compute number of new blocks that are requested size_t new_blocks = (n_bytes + BYTES_PER_BLOCK - 1) / BYTES_PER_BLOCK; // Get the total number of consecutive blocks that are already allocated to // this chunk of memory, and then count the number of free blocks following // it. Stop if we reach the end of the heap, or if we find enough extra // free blocks to satisfy the realloc. Note that we need to compute the // total size of the existing memory chunk so we can correctly and // efficiently shrink it (see below for shrinking code). size_t n_free = 0; size_t n_blocks = 1; // counting HEAD block size_t max_block = MP_STATE_MEM(gc_alloc_table_byte_len) * BLOCKS_PER_ATB; for (size_t bl = block + n_blocks; bl < max_block; bl++) { byte block_type = ATB_GET_KIND(bl); if (block_type == AT_TAIL) { n_blocks++; continue; } if (block_type == AT_FREE) { n_free++; if (n_blocks + n_free >= new_blocks) { // stop as soon as we find enough blocks for n_bytes break; } continue; } break; } // return original ptr if it already has the requested number of blocks if (new_blocks == n_blocks) { return ptr_in; } // check if we can shrink the allocated area if (new_blocks < n_blocks) { // free unneeded tail blocks for (size_t bl = block + new_blocks, count = n_blocks - new_blocks; count > 0; bl++, count--) { ATB_ANY_TO_FREE(bl); } // set the last_free pointer to end of this block if it's earlier in the heap if ((block + new_blocks) / BLOCKS_PER_ATB < MP_STATE_MEM(gc_last_free_atb_index)) { MP_STATE_MEM(gc_last_free_atb_index) = (block + new_blocks) / BLOCKS_PER_ATB; } #if EXTENSIVE_HEAP_PROFILING gc_dump_alloc_table(); #endif return ptr_in; } // check if we can expand in place if (new_blocks <= n_blocks + n_free) { // mark few more blocks as used tail for (size_t bl = block + n_blocks; bl < block + new_blocks; bl++) { assert(ATB_GET_KIND(bl) == AT_FREE); ATB_FREE_TO_TAIL(bl); } // zero out the bytes of the newly allocated blocks (see comment above in gc_alloc) memset((byte*)ptr_in + n_blocks * BYTES_PER_BLOCK, 0, (new_blocks - n_blocks) * BYTES_PER_BLOCK); #if EXTENSIVE_HEAP_PROFILING gc_dump_alloc_table(); #endif return ptr_in; } if (!allow_move) { // not allowed to move memory block so return failure return NULL; } // can't resize inplace; try to find a new contiguous chain void *ptr_out = gc_alloc(n_bytes, #if MICROPY_ENABLE_FINALISER FTB_GET(block) #else false #endif ); // check that the alloc succeeded if (ptr_out == NULL) { return NULL; } DEBUG_printf("gc_realloc(%p -> %p)\n", ptr_in, ptr_out); memcpy(ptr_out, ptr_in, n_blocks * BYTES_PER_BLOCK); gc_free(ptr_in); return ptr_out; }
CSSM_RETURN dlbe_CreateFiles(DATABASE_BACKEND_HANDLE hDatabase, TABLE_BACKEND_REF *prTableBackend, CSSM_DB_RECORDTYPE Recordtype, const DAL_TRANSLATION_TABLE * pTable, DAL_MODULE_PARAMETERS * Parameters) { CSSM_RETURN ret; char FileMode[8]; void *AccessCtx = NULL; CSSM_DATA_PTR Passphrase = NULL; FF_FUNCTION_BEGIN(dlbe_CreateTable); VERIFY_PTR(hDatabase); VERIFY_PTR(prTableBackend); VERIFY_PTR(pTable); VERIFY_PTR(Parameters); /*DATABASE_BACKEND* pDatabase = (DATABASE_BACKEND*)hDatabase;*/ PATH_NAME IndexPath, DataPath, FreeListPath; fff_static_nrCreatePathNames(IndexPath, DataPath, FreeListPath, GetPathStart(Parameters), Recordtype); uint32 NumIndexes = pTable->neGetIndexCount(); uint32 NumAttributes = pTable->neGetPureAttributeCount(); /* Write out the files */ /* * Setup permissions for index file and write it out. */ strcpy (FileMode, "wb"); #if MODULE_PARAMETERS_USE_FUNCTION_PARAMETERS /* if (Parameters->UserAuthentication) Passphrase = Parameters->UserAuthentication->Credential; else */ Passphrase = NULL; if ((ret = ffport_SetupFilePermissionsStart(Passphrase, IndexPath, Parameters->DbLocation, FileMode, &AccessCtx)) != CSSM_OK) { return ret; } #endif ret = ff_index_WriteNewFile(IndexPath, NumIndexes, NumAttributes, (const char *)FileMode); #if MODULE_PARAMETERS_USE_FUNCTION_PARAMETERS if (((ret = ffport_SetupFilePermissionsEnd (AccessCtx)) != CSSM_OK) || (CSSM_OK != ret)) { return ret; } #else if (CSSM_OK != ret) return ret; #endif /* * Setup permissions for freelist file and write it out. */ #if MODULE_PARAMETERS_USE_FUNCTION_PARAMETERS if ((ret = ffport_SetupFilePermissionsStart(Passphrase, FreeListPath, Parameters->DbLocation, FileMode, &AccessCtx)) != CSSM_OK) { return ret; } #endif ret = ff_freeList_WriteNewFile(FreeListPath, (const char *)FileMode); #if MODULE_PARAMETERS_USE_FUNCTION_PARAMETERS if (((ret = ffport_SetupFilePermissionsEnd (AccessCtx)) != CSSM_OK) || (CSSM_OK != ret)) { return ret; } #else if (CSSM_OK != ret) return ret; #endif /* * Setup permissions for data file and write it out. */ #if MODULE_PARAMETERS_USE_FUNCTION_PARAMETERS if ((ret = ffport_SetupFilePermissionsStart(Passphrase, DataPath, Parameters->DbLocation, FileMode, &AccessCtx)) != CSSM_OK) { return ret; } #endif ret = ff_data_WriteNewFile(DataPath, NumIndexes + NumAttributes + 1, (const char *)FileMode); #if MODULE_PARAMETERS_USE_FUNCTION_PARAMETERS if (((ret = ffport_SetupFilePermissionsEnd (AccessCtx)) != CSSM_OK) || (CSSM_OK != ret)) { return ret; } #else if (CSSM_OK != ret) return ret; #endif return CSSM_OK; }
/*----------------------------------------------------------------------------- * Name: dlbe_OpenTable * * Description: * Will allocate memory for a table and then open it up using flat file commands * * Parameters: * FUNCTION_ARG (input/output) * * Return value: * CSSM_OK on success, otherwise CSSM_FAIL * * Error Codes: * CSSMERR_DL_INTERNAL_ERROR (should never be returned) * CSSMERR_DL_MEMORY_ERROR *---------------------------------------------------------------------------*/ CSSM_RETURN dlbe_OpenTable(DATABASE_BACKEND_HANDLE hDatabase, TABLE_BACKEND_REF *prTableBackend, CSSM_DB_RECORDTYPE Recordtype, const DAL_TRANSLATION_TABLE *pTranslationTable, const DAL_MODULE_PARAMETERS * Parameters) { uint32 Error; CSSM_RETURN ret = CSSM_OK; CSSM_DB_ACCESS_TYPE AccessFilter = 0; CSSM_DATA_PTR Passphrase = NULL; FF_FUNCTION_BEGIN(dlbe_OpenTable); VERIFY_PTR(prTableBackend); VERIFY_PTR(hDatabase); VERIFY_PTR(pTranslationTable); /*DATABASE_BACKEND* pDatabase = (DATABASE_BACKEND*)hDatabase;*/ FLATFILE_TABLE_BACKEND_REF rFlatfileBackend = NULL; const char *PathStart = GetPathStart(Parameters); ASSERT(PathStart); PATH_NAME IndexPath, DataPath, FreeListPath; fff_static_nrCreatePathNames(IndexPath, DataPath, FreeListPath, PathStart, Recordtype); if (CSSM_FALSE == ffport_neDoesFileExist(IndexPath)) return CSSMERR_DL_DATABASE_CORRUPT; #if MODULE_PARAMETERS_USE_FUNCTION_PARAMETERS /* if (Parameters->UserAuthentication) Passphrase = Parameters->UserAuthentication->Credential; else */ Passphrase = NULL; if (CSSM_FALSE == ffport_IsValidFilePermissions ( Passphrase, IndexPath, Parameters->AccessRequest, AccessFilter, &Error)) return CSSMERR_DL_OS_ACCESS_DENIED; #endif if (CSSM_FALSE == ffport_neDoesFileExist(FreeListPath)) return CSSMERR_DL_DATABASE_CORRUPT; #if MODULE_PARAMETERS_USE_FUNCTION_PARAMETERS if (CSSM_FALSE == ffport_IsValidFilePermissions ( Passphrase, FreeListPath, Parameters->AccessRequest, AccessFilter, &Error)) return CSSMERR_DL_OS_ACCESS_DENIED; #endif if (CSSM_FALSE == ffport_neDoesFileExist(DataPath)) return CSSMERR_DL_DATABASE_CORRUPT; #if MODULE_PARAMETERS_USE_FUNCTION_PARAMETERS if (CSSM_FALSE == ffport_IsValidFilePermissions ( Passphrase, DataPath, Parameters->AccessRequest, AccessFilter, &Error)) return CSSMERR_DL_OS_ACCESS_DENIED; #endif rFlatfileBackend = new FLATFILE_TABLE_BACKEND; *prTableBackend = (TABLE_BACKEND*)rFlatfileBackend; /* Cast to base class */ if (*prTableBackend == NULL) return CSSMERR_DL_MEMORY_ERROR; uint32 NumIndexes = pTranslationTable->neGetIndexCount(); uint32 NumAttributes = pTranslationTable->neGetPureAttributeCount(); ret = (rFlatfileBackend)->Initialize(pTranslationTable, NumIndexes, NumAttributes, Recordtype, IndexPath, FreeListPath, DataPath, Parameters); if (CSSM_OK != ret) { dlbe_DestroyTable (*prTableBackend); *prTableBackend = NULL; } return ret; }