/* * Reset the pool to the state when it was created. * All blocks will be deallocated except the first block. All memory areas * are marked as free. */ static void reset_pool(pj_pool_t *pool) { pj_pool_block *block; PJ_CHECK_STACK(); block = pool->block_list.prev; if (block == &pool->block_list) return; /* Skip the first block because it is occupying the same memory as the pool itself. */ block = block->prev; while (block != &pool->block_list) { pj_pool_block *prev = block->prev; pj_list_erase(block); (*pool->factory->policy.block_free)(pool->factory, block, block->end - (unsigned char*)block); block = prev; } block = pool->block_list.next; /* Set the start pointer, aligning it as needed */ block->cur = ALIGN_PTR(block->buf, PJ_POOL_ALIGNMENT); pool->capacity = block->end - (unsigned char*)pool; }
MemoryPool* initPool(size_t size, size_t increment) { MemoryPool *pool; MemoryBlock *block; void *buffer; ASSERT_RETURN(size >= sizeof(MemoryPool)+sizeof(MemoryBlock), NULL); buffer=malloc(size+sizeof(*pool)+sizeof(*block)); if (!buffer) return NULL; // Initialize first memory block block = (MemoryBlock*) ((size_t)buffer+sizeof(*pool)); block->next = NULL; block->start=(void*)((size_t)block+sizeof(*block)); block->pos=ALIGN_PTR(block->start, POOL_ALIGNMENT); block->end=block->start+size; // Initialize pool pool = (MemoryPool*) buffer; pool->lastAlloc = 0; pool->capacity = size; pool->increment = (increment) ? increment : (size_t)sysconf(_SC_PAGESIZE); pool->base=block; pool->cur=block; PRINT("Pool created, used size=%ld, capacity=%ld, increment size=%ld", (((size_t)(pool->cur->start)) - (size_t)pool), pool->capacity, pool->increment); return pool; }
static void* msvcrt_heap_realloc(DWORD flags, void *ptr, MSVCRT_size_t size) { if(sb_heap && ptr && !HeapValidate(heap, 0, ptr)) { /* TODO: move data to normal heap if it exceeds sbh_threshold limit */ void *memblock, *temp, **saved; MSVCRT_size_t old_padding, new_padding, old_size; saved = SAVED_PTR(ptr); old_padding = (char*)ptr - (char*)*saved; old_size = HeapSize(sb_heap, 0, *saved); if(old_size == -1) return NULL; old_size -= old_padding; temp = HeapReAlloc(sb_heap, flags, *saved, size+sizeof(void*)+SB_HEAP_ALIGN); if(!temp) return NULL; memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0); saved = SAVED_PTR(memblock); new_padding = (char*)memblock - (char*)temp; if(new_padding != old_padding) memmove(memblock, (char*)temp+old_padding, old_size>size ? size : old_size); *saved = temp; return memblock; } return HeapReAlloc(heap, flags, ptr, size); }
/* * Create new block. * Create a new big chunk of memory block, from which user allocation will be * taken from. */ static pj_pool_block *pj_pool_create_block( pj_pool_t *pool, pj_size_t size) { pj_pool_block *block; PJ_CHECK_STACK(); pj_assert(size >= sizeof(pj_pool_block)); LOG((pool->obj_name, "create_block(sz=%u), cur.cap=%u, cur.used=%u", size, pool->capacity, pj_pool_get_used_size(pool))); /* Request memory from allocator. */ block = (pj_pool_block*) (*pool->factory->policy.block_alloc)(pool->factory, size); if (block == NULL) { (*pool->callback)(pool, size); return NULL; } /* Add capacity. */ pool->capacity += size; /* Set start and end of buffer. */ block->buf = ((unsigned char*)block) + sizeof(pj_pool_block); block->end = ((unsigned char*)block) + size; /* Set the start pointer, aligning it as needed */ block->cur = ALIGN_PTR(block->buf, PJ_POOL_ALIGNMENT); /* Insert in the front of the list. */ pj_list_insert_after(&pool->block_list, block); LOG((pool->obj_name," block created, buffer=%p-%p",block->buf, block->end)); return block; }
/** * Allocate memory from hunk managed by the UI code * This memory is allocated one time, and never released. * @param size Quantity of memory expected * @param align Alignement of the expected memory * @param reset If true initilize the memory with 0 * @return available memory, else nullptr */ void* UI_AllocHunkMemory (size_t size, int align, bool reset) { byte* memory = (byte*) ALIGN_PTR(ui_global.curadata, align); if (memory + size > ui_global.adata + ui_global.adataize) return nullptr; if (reset) memset(memory, 0, size); ui_global.curadata = memory + size; return memory; }
void rst_endevent(rst_buffer_t * ptr) { ptr->rst_buffer_ptr = ALIGN_PTR(ptr->rst_buffer_ptr); if (RST_BUF_COUNT(ptr) > (RST_BUF_SIZE(ptr) - RST_MAX_EVENT_SIZE)) { fprintf(stderr, "librastro: Buffer size exceeded, flushing to disk. " "Consider using a larger buffer size, defined by the environment " "variable RST_BUFFER_SIZE\n"); rst_flush(ptr); } }
void *FDKaalloc_L(const UINT size, const UINT alignment, MEMORY_SECTION s) { void *addr, *result=NULL; addr = FDKcalloc_L(1, size + alignment + sizeof(void*), s); /* Malloc and clear memory. */ if (addr!=NULL) { result = ALIGN_PTR((unsigned char *)addr + sizeof(void*)); /* Get aligned memory base address. */ *(((void**)result) - 1) = addr; /* Save malloc'ed memory pointer. */ } return result; /* Return aligned address. */ }
void releasePool(MemoryPool *pool) { MemoryBlock *next=pool->base->next; // Skip first entry while(next!=NULL) { MemoryBlock *cur=next; next=next->next; pool->capacity -= (size_t)cur->end - (size_t)cur->start; free(cur); } pool->base->pos=ALIGN_PTR(pool->base->start, POOL_ALIGNMENT); pool->cur=pool->base; PRINT("Release pool, used size=%ld, capacity=%ld, increment size=%ld", (((size_t)(pool->cur->start)) - (size_t)pool), pool->capacity, pool->increment); }
/* * Create new memory pool. */ PJ_DEF(pj_pool_t*) pj_pool_create_int( pj_pool_factory *f, const char *name, pj_size_t initial_size, pj_size_t increment_size, pj_pool_callback *callback) { pj_pool_t *pool; pj_pool_block *block; pj_uint8_t *buffer; PJ_CHECK_STACK(); /* Size must be at least sizeof(pj_pool)+sizeof(pj_pool_block) */ PJ_ASSERT_RETURN(initial_size >= sizeof(pj_pool_t)+sizeof(pj_pool_block), NULL); /* If callback is NULL, set calback from the policy */ if (callback == NULL) callback = f->policy.callback; /* Allocate initial block */ buffer = (pj_uint8_t*) (*f->policy.block_alloc)(f, initial_size); if (!buffer) return NULL; /* Set pool administrative data. */ pool = (pj_pool_t*)buffer; pj_bzero(pool, sizeof(*pool)); pj_list_init(&pool->block_list); pool->factory = f; /* Create the first block from the memory. */ block = (pj_pool_block*) (buffer + sizeof(*pool)); block->buf = ((unsigned char*)block) + sizeof(pj_pool_block); block->end = buffer + initial_size; /* Set the start pointer, aligning it as needed */ block->cur = ALIGN_PTR(block->buf, PJ_POOL_ALIGNMENT); pj_list_insert_after(&pool->block_list, block); pj_pool_init_int(pool, name, increment_size, callback); /* Pool initial capacity and used size */ pool->capacity = initial_size; LOG((pool->obj_name, "pool created, size=%u", pool->capacity)); return pool; }
void poolGrowTo(MemoryPool *pool, size_t size) { assert(size >= sizeof(MemoryBlock)); MemoryBlock *block=malloc(size+sizeof(MemoryBlock)); assert(block!=NULL); block->next = NULL; block->start=(void*)((size_t)block+sizeof(*block)); block->pos=ALIGN_PTR(block->start, POOL_ALIGNMENT); block->end=(void*)((size_t)block->start+size); pool->cur->next = block; pool->cur=block; pool->capacity+=size; PRINT("Grow to %ld, new capacity=%ld, new block %p, size block=%ld", size, pool->capacity, block, (size_t)block->end-(size_t)block->start); }
static void* msvcrt_heap_alloc(DWORD flags, MSVCRT_size_t size) { if(size < MSVCRT_sbh_threshold) { void *memblock, *temp, **saved; temp = HeapAlloc(sb_heap, flags, size+sizeof(void*)+SB_HEAP_ALIGN); if(!temp) return NULL; memblock = ALIGN_PTR(temp, SB_HEAP_ALIGN, 0); saved = SAVED_PTR(memblock); *saved = temp; return memblock; } return HeapAlloc(heap, flags, size); }
/** * @brief Allocate a node into the UI memory (do notr call behaviour->new) * @note It's not a dynamic memory allocation. Please only use it at the loading time * @todo Assert out when we are not in parsing/loading stage * @param[in] name Name of the new node, else NULL if we dont want to edit it. * @param[in] type Name of the node behavior * @param[in] isDynamic Allocate a node in static or dynamic memory */ static uiNode_t* UI_AllocNodeWithoutNew (const char* name, const char* type, qboolean isDynamic) { uiNode_t* node; uiBehaviour_t *behaviour; int nodeSize; behaviour = UI_GetNodeBehaviour(type); if (behaviour == NULL) Com_Error(ERR_FATAL, "UI_AllocNodeWithoutNew: Node behaviour '%s' doesn't exist", type); nodeSize = sizeof(*node) + behaviour->extraDataSize; if (!isDynamic) { if (ui_global.curadata + nodeSize > ui_global.adata + ui_global.adataize) Com_Error(ERR_FATAL, "UI_AllocNodeWithoutNew: No more memory to allocate a new node"); /** @todo fix this hard coded '8' value */ ui_global.curadata = ALIGN_PTR(ui_global.curadata, 8); node = (uiNode_t*) ui_global.curadata; ui_global.curadata += nodeSize; ui_global.numNodes++; memset(node, 0, nodeSize); } else { node = (uiNode_t*)Mem_PoolAlloc(nodeSize, ui_dynPool, 0); memset(node, 0, nodeSize); node->dynamic = qtrue; } node->behaviour = behaviour; #ifdef DEBUG node->behaviour->count++; #endif if (node->behaviour->isAbstract) Com_Error(ERR_FATAL, "UI_AllocNodeWithoutNew: Node behavior '%s' is abstract. We can't instantiate it.", type); if (name != NULL) { Q_strncpyz(node->name, name, sizeof(node->name)); if (strlen(node->name) != strlen(name)) Com_Printf("UI_AllocNodeWithoutNew: Node name \"%s\" truncated. New name is \"%s\"\n", name, node->name); } /* initialize default properties */ if (node->behaviour->loading) node->behaviour->loading(node); return node; }
/********************************************************************* * _aligned_offset_malloc (MSVCRT.@) */ void * CDECL _aligned_offset_malloc(MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset) { void *memblock, *temp, **saved; TRACE("(%lu, %lu, %lu)\n", size, alignment, offset); /* alignment must be a power of 2 */ if ((alignment & (alignment - 1)) != 0) { *MSVCRT__errno() = MSVCRT_EINVAL; return NULL; } /* offset must be less than size */ if (offset >= size) { *MSVCRT__errno() = MSVCRT_EINVAL; return NULL; } /* don't align to less than void pointer size */ if (alignment < sizeof(void *)) alignment = sizeof(void *); /* allocate enough space for void pointer and alignment */ temp = MSVCRT_malloc(size + alignment + sizeof(void *)); if (!temp) return NULL; /* adjust pointer for proper alignment and offset */ memblock = ALIGN_PTR(temp, alignment, offset); /* Save the real allocation address below returned address */ /* so it can be found later to free. */ saved = SAVED_PTR(memblock); *saved = temp; return memblock; }
void *mem_pool_alloc(mem_pool_t *mem_pool, int size) { char *ptr = NULL; mem_page_t *current = NULL; mem_page_t *new_page = NULL; void *addr = NULL; if(!mem_pool){ printf("mem_pool is null.\n"); return NULL; } /* 先判断size的大小是否超出了page size */ if(size > mem_pool->page_size - (int)sizeof(mem_page_t)){ printf("size > %lu.\n", mem_pool->page_size - sizeof(mem_page_t)); return NULL; } #ifdef MEM_POOL_LOCK pthread_mutex_lock(&mem_pool->lock); #endif /* 从现有的page链表中寻找空间分配 */ current = mem_pool->current; while(current){ ptr = ALIGN_PTR(current->last, PLATFORM_ALIGNMENT); /* 如果空间足够 */ if((char *)current->end - ptr >= size){ current->last = ptr + size; #ifdef MEM_POOL_LOCK pthread_mutex_unlock(&mem_pool->lock); #endif return ptr; } current->failed++; if(current->failed >= MAX_FAILED_COUNT){ mem_pool->current = current->next; } current = current->next; } /* 现有的page空间没有足够的空间分配,必须创建新的page */ if(posix_memalign(&addr, mem_pool->page_size, mem_pool->page_size) != 0){ printf("posix_memalign in mem_pool_alloc fails.\n"); #ifdef MEM_POOL_LOCK pthread_mutex_unlock(&mem_pool->lock); #endif return NULL; } new_page = (mem_page_t *)addr; new_page->start = (char *)addr; new_page->last = new_page->start + sizeof(mem_page_t); new_page->end = (char *)addr + mem_pool->page_size; new_page->next = NULL; new_page->failed = 0; mem_pool->page_count++; /* 将新增加的page添加到page的链表的末尾 */ mem_pool->tail->next = new_page; mem_pool->tail = new_page; if(NULL == mem_pool->current){ mem_pool->current = new_page; } ptr = ALIGN_PTR(new_page->last, PLATFORM_ALIGNMENT); new_page->last = ptr + size; #ifdef MEM_POOL_LOCK pthread_mutex_unlock(&mem_pool->lock); #endif return ptr; }
static xptr extend_file_helper(UFile fileHandle, int64_t fileSizeCurrent, int64_t fileSizeNew, xptr freeStackHd, t_layer layerAdjustment, int64_t offsAdjustment, bool initializeData) { /* Does it make sense to initialize every block header? I doubt since when a block is allocated it is never read back from HDD, instead header is initialized when block is put to buffer. */ if (initializeData) { int64_t spanBeginOffs = fileSizeCurrent; int64_t spanEndOffs = fileSizeNew; int64_t offset; void *buf = NULL; vmm_sm_blk_hdr *hdr = NULL; unsigned int bytesOutCnt = 0; /* Allocate aligned buffer and initialize header struct. */ buf = malloc(VMM_SM_BLK_HDR_MAX_SIZE * 2); if (!buf) throw std::bad_alloc(); memset(buf, 0, VMM_SM_BLK_HDR_MAX_SIZE); hdr = (vmm_sm_blk_hdr *)ALIGN_PTR(buf, VMM_SM_BLK_HDR_MAX_SIZE); vmm_sm_blk_hdr::init(hdr); /* Make offsets aligned on block boundary. */ FixSpan(&spanBeginOffs, &spanEndOffs); /* Visit every block. */ while (1) { offset = spanBeginOffs; hdr->p = ConsumeNextBlockInSpan( &spanBeginOffs, &spanEndOffs, layerAdjustment, offsAdjustment, CONSUME_NORMAL); /* All done? */ if (hdr->p == XNULL) break; /* Seek to offset. */ if (!uSetFilePointer( fileHandle, offset, NULL, U_FILE_BEGIN, __sys_call_error)) { free(buf); throw SYSTEM_ENV_EXCEPTION("Cannot set file pointer"); } /* Write header. */ if (!uWriteFile( fileHandle, hdr, VMM_SM_BLK_HDR_MAX_SIZE, &bytesOutCnt, __sys_call_error) || bytesOutCnt!=VMM_SM_BLK_HDR_MAX_SIZE) { free(buf); throw SYSTEM_ENV_EXCEPTION("Cannot write to file"); } } /* Dismiss buffer. */ hdr = NULL; free(buf); buf = NULL; } add_file_span_to_pfb_stack( &freeStackHd, fileSizeCurrent, fileSizeNew, layerAdjustment, offsAdjustment); return freeStackHd; }
void* LinearAllocator::start(Page* p) { return ALIGN_PTR(((char*)p) + sizeof(Page)); }
void* LinearAllocator::start(Page* p) { return ALIGN_PTR((size_t)p + sizeof(Page)); }
INT CLpd_FAC_Acelp2Mdct(H_MDCT hMdct, FIXP_DBL *output, FIXP_DBL *_pSpec, const SHORT spec_scale[], const int nSpec, FIXP_DBL *pFac, const int fac_scale, const INT fac_length, INT noOutSamples, const INT tl, const FIXP_WTP *wrs, const INT fr, FIXP_LPC A[16], INT A_exp, CAcelpStaticMem *acelp_mem, const FIXP_DBL gain, const int last_frame_lost, const int isFdFac, const UCHAR last_lpd_mode, const int k, int currAliasingSymmetry) { FIXP_DBL *pCurr, *pOvl, *pSpec; const FIXP_WTP *pWindow; const FIXP_WTB *FacWindowZir_conceal; UCHAR doFacZirConceal = 0; int doDeemph = 1; const FIXP_WTB *FacWindowZir, *FacWindowSynth; FIXP_DBL *pOut0 = output, *pOut1; int w, i, fl, nl, nr, f_len, nrSamples = 0, s = 0, scale, total_gain_e; FIXP_DBL *pF, *pFAC_and_FAC_ZIR = NULL; FIXP_DBL total_gain = gain; FDK_ASSERT(fac_length <= 1024 / (4 * 2)); switch (fac_length) { /* coreCoderFrameLength = 1024 */ case 128: pWindow = SineWindow256; FacWindowZir = FacWindowZir128; FacWindowSynth = FacWindowSynth128; break; case 64: pWindow = SineWindow128; FacWindowZir = FacWindowZir64; FacWindowSynth = FacWindowSynth64; break; case 32: pWindow = SineWindow64; FacWindowZir = FacWindowZir32; FacWindowSynth = FacWindowSynth32; break; /* coreCoderFrameLength = 768 */ case 96: pWindow = SineWindow192; FacWindowZir = FacWindowZir96; FacWindowSynth = FacWindowSynth96; break; case 48: pWindow = SineWindow96; FacWindowZir = FacWindowZir48; FacWindowSynth = FacWindowSynth48; break; default: FDK_ASSERT(0); return 0; } FacWindowZir_conceal = FacWindowSynth; /* Derive NR and NL */ fl = fac_length * 2; nl = (tl - fl) >> 1; nr = (tl - fr) >> 1; if (noOutSamples > nrSamples) { /* Purge buffered output. */ FDKmemcpy(pOut0, hMdct->overlap.time, hMdct->ov_offset * sizeof(pOut0[0])); nrSamples = hMdct->ov_offset; hMdct->ov_offset = 0; } if (nrSamples >= noOutSamples) { pOut1 = hMdct->overlap.time + hMdct->ov_offset; if (hMdct->ov_offset < fac_length) { pOut0 = output + nrSamples; } else { pOut0 = pOut1; } hMdct->ov_offset += fac_length + nl; } else { pOut1 = output + nrSamples; pOut0 = output + nrSamples; } { pFAC_and_FAC_ZIR = CLpd_ACELP_GetFreeExcMem(acelp_mem, 2 * fac_length); { const FIXP_DBL *pTmp1, *pTmp2; doFacZirConceal |= ((last_frame_lost != 0) && (k == 0)); doDeemph &= (last_lpd_mode != 4); if (doFacZirConceal) { /* ACELP contribution in concealment case: Use ZIR with a modified ZIR window to preserve some more energy. Dont use FAC, which contains wrong information for concealed frame Dont use last ACELP samples, but double ZIR, instead (afterwards) */ FDKmemclear(pFAC_and_FAC_ZIR, 2 * fac_length * sizeof(FIXP_DBL)); FacWindowSynth = (FIXP_WTB *)pFAC_and_FAC_ZIR; FacWindowZir = FacWindowZir_conceal; } else { CFac_CalcFacSignal(pFAC_and_FAC_ZIR, pFac, fac_scale + s, fac_length, A, A_exp, 1, isFdFac); } /* 6) Get windowed past ACELP samples and ACELP ZIR signal */ /* * Get ACELP ZIR (pFac[]) and ACELP past samples (pOut0[]) and add them * to the FAC synth signal contribution on pOut1[]. */ { { CLpd_Acelp_Zir(A, A_exp, acelp_mem, fac_length, pFac, doDeemph); pTmp1 = pOut0; pTmp2 = pFac; } for (i = 0, w = 0; i < fac_length; i++) { FIXP_DBL x; /* Div2 is compensated by table scaling */ x = fMultDiv2(pTmp2[i], FacWindowZir[w]); x += fMultDiv2(pTmp1[-i - 1], FacWindowSynth[w]); x += pFAC_and_FAC_ZIR[i]; pOut1[i] = x; w++; } } if (doFacZirConceal) { /* ZIR is the only ACELP contribution, so double it */ scaleValues(pOut1, fac_length, 1); } } } if (nrSamples < noOutSamples) { nrSamples += fac_length + nl; } /* Obtain transform gain */ total_gain = gain; total_gain_e = 0; imdct_gain(&total_gain, &total_gain_e, tl); /* IMDCT overlap add */ scale = total_gain_e; pSpec = _pSpec; /* Note:when comming from an LPD frame (TCX/ACELP) the previous alisaing * symmetry must always be 0 */ if (currAliasingSymmetry == 0) { dct_IV(pSpec, tl, &scale); } else { FIXP_DBL _tmp[1024 + ALIGNMENT_DEFAULT / sizeof(FIXP_DBL)]; FIXP_DBL *tmp = (FIXP_DBL *)ALIGN_PTR(_tmp); C_ALLOC_ALIGNED_REGISTER(tmp, sizeof(_tmp)); dst_III(pSpec, tmp, tl, &scale); C_ALLOC_ALIGNED_UNREGISTER(tmp); } /* Optional scaling of time domain - no yet windowed - of current spectrum */ if (total_gain != (FIXP_DBL)0) { for (i = 0; i < tl; i++) { pSpec[i] = fMult(pSpec[i], total_gain); } } int loc_scale = fixmin_I(spec_scale[0] + scale, (INT)DFRACT_BITS - 1); scaleValuesSaturate(pSpec, tl, loc_scale); pOut1 += fl / 2 - 1; pCurr = pSpec + tl - fl / 2; for (i = 0; i < fl / 2; i++) { FIXP_DBL x1; /* FAC signal is already on pOut1, because of that the += operator. */ x1 = fMult(*pCurr++, pWindow[i].v.re); FDK_ASSERT((pOut1 >= hMdct->overlap.time && pOut1 < hMdct->overlap.time + hMdct->ov_size) || (pOut1 >= output && pOut1 < output + 1024)); *pOut1 += IMDCT_SCALE_DBL(-x1); pOut1--; } /* NL output samples TL/2+FL/2..TL. - current[FL/2..0] */ pOut1 += (fl / 2) + 1; pFAC_and_FAC_ZIR += fac_length; /* set pointer to beginning of FAC ZIR */ if (nl == 0) { /* save pointer to write FAC ZIR data later */ hMdct->pFacZir = pFAC_and_FAC_ZIR; } else { FDK_ASSERT(nl >= fac_length); /* FAC ZIR will be added now ... */ hMdct->pFacZir = NULL; } pF = pFAC_and_FAC_ZIR; f_len = fac_length; pCurr = pSpec + tl - fl / 2 - 1; for (i = 0; i < nl; i++) { FIXP_DBL x = -(*pCurr--); /* 5) (item 4) Synthesis filter Zir component, FAC ZIR (another one). */ if (i < f_len) { x += *pF++; } FDK_ASSERT((pOut1 >= hMdct->overlap.time && pOut1 < hMdct->overlap.time + hMdct->ov_size) || (pOut1 >= output && pOut1 < output + 1024)); *pOut1 = IMDCT_SCALE_DBL(x); pOut1++; } hMdct->prev_nr = nr; hMdct->prev_fr = fr; hMdct->prev_wrs = wrs; hMdct->prev_tl = tl; hMdct->prevPrevAliasSymmetry = hMdct->prevAliasSymmetry; hMdct->prevAliasSymmetry = currAliasingSymmetry; fl = fr; nl = nr; pOvl = pSpec + tl / 2 - 1; pOut0 = pOut1; for (w = 1; w < nSpec; w++) /* for ACELP -> FD short */ { const FIXP_WTP *pWindow_prev; /* Setup window pointers */ pWindow_prev = hMdct->prev_wrs; /* Current spectrum */ pSpec = _pSpec + w * tl; scale = total_gain_e; /* For the second, third, etc. short frames the alisaing symmetry is equal, * either (0,0) or (1,1) */ if (currAliasingSymmetry == 0) { /* DCT IV of current spectrum */ dct_IV(pSpec, tl, &scale); } else { dst_IV(pSpec, tl, &scale); } /* Optional scaling of time domain - no yet windowed - of current spectrum */ /* and de-scale current spectrum signal (time domain, no yet windowed) */ if (total_gain != (FIXP_DBL)0) { for (i = 0; i < tl; i++) { pSpec[i] = fMult(pSpec[i], total_gain); } } loc_scale = fixmin_I(spec_scale[w] + scale, (INT)DFRACT_BITS - 1); scaleValuesSaturate(pSpec, tl, loc_scale); if (noOutSamples <= nrSamples) { /* Divert output first half to overlap buffer if we already got enough * output samples. */ pOut0 = hMdct->overlap.time + hMdct->ov_offset; hMdct->ov_offset += hMdct->prev_nr + fl / 2; } else { /* Account output samples */ nrSamples += hMdct->prev_nr + fl / 2; } /* NR output samples 0 .. NR. -overlap[TL/2..TL/2-NR] */ for (i = 0; i < hMdct->prev_nr; i++) { FIXP_DBL x = -(*pOvl--); *pOut0 = IMDCT_SCALE_DBL(x); pOut0++; } if (noOutSamples <= nrSamples) { /* Divert output second half to overlap buffer if we already got enough * output samples. */ pOut1 = hMdct->overlap.time + hMdct->ov_offset + fl / 2 - 1; hMdct->ov_offset += fl / 2 + nl; } else { pOut1 = pOut0 + (fl - 1); nrSamples += fl / 2 + nl; } /* output samples before window crossing point NR .. TL/2. * -overlap[TL/2-NR..TL/2-NR-FL/2] + current[NR..TL/2] */ /* output samples after window crossing point TL/2 .. TL/2+FL/2. * -overlap[0..FL/2] - current[TL/2..FL/2] */ pCurr = pSpec + tl - fl / 2; if (currAliasingSymmetry == 0) { for (i = 0; i < fl / 2; i++) { FIXP_DBL x0, x1; cplxMult(&x1, &x0, *pCurr++, -*pOvl--, pWindow_prev[i]); *pOut0 = IMDCT_SCALE_DBL(x0); *pOut1 = IMDCT_SCALE_DBL(-x1); pOut0++; pOut1--; } } else { if (hMdct->prevPrevAliasSymmetry == 0) { /* Jump DST II -> DST IV for the second window */ for (i = 0; i < fl / 2; i++) { FIXP_DBL x0, x1; cplxMult(&x1, &x0, *pCurr++, -*pOvl--, pWindow_prev[i]); *pOut0 = IMDCT_SCALE_DBL(x0); *pOut1 = IMDCT_SCALE_DBL(x1); pOut0++; pOut1--; } } else { /* Jump DST IV -> DST IV from the second window on */ for (i = 0; i < fl / 2; i++) { FIXP_DBL x0, x1; cplxMult(&x1, &x0, *pCurr++, *pOvl--, pWindow_prev[i]); *pOut0 = IMDCT_SCALE_DBL(x0); *pOut1 = IMDCT_SCALE_DBL(x1); pOut0++; pOut1--; } } } if (hMdct->pFacZir != 0) { /* add FAC ZIR of previous ACELP -> mdct transition */ FIXP_DBL *pOut = pOut0 - fl / 2; FDK_ASSERT(fl / 2 <= 128); for (i = 0; i < fl / 2; i++) { pOut[i] += IMDCT_SCALE_DBL(hMdct->pFacZir[i]); } hMdct->pFacZir = NULL; } pOut0 += (fl / 2); /* NL output samples TL/2+FL/2..TL. - current[FL/2..0] */ pOut1 += (fl / 2) + 1; pCurr = pSpec + tl - fl / 2 - 1; for (i = 0; i < nl; i++) { FIXP_DBL x = -(*pCurr--); *pOut1 = IMDCT_SCALE_DBL(x); pOut1++; } /* Set overlap source pointer for next window pOvl = pSpec + tl/2 - 1; */ pOvl = pSpec + tl / 2 - 1; /* Previous window values. */ hMdct->prev_nr = nr; hMdct->prev_fr = fr; hMdct->prev_tl = tl; hMdct->prev_wrs = pWindow_prev; hMdct->prevPrevAliasSymmetry = hMdct->prevAliasSymmetry; hMdct->prevAliasSymmetry = currAliasingSymmetry; } /* Save overlap */ pOvl = hMdct->overlap.freq + hMdct->ov_size - tl / 2; FDK_ASSERT(pOvl >= hMdct->overlap.time + hMdct->ov_offset); FDK_ASSERT(tl / 2 <= hMdct->ov_size); for (i = 0; i < tl / 2; i++) { pOvl[i] = _pSpec[i + (w - 1) * tl]; } return nrSamples; }
dpa_t *dpa_init(uint32_t hypotheses, uint32_t tracelen) { size_t memsize; correl_t *ptr; dpa_t *dpa = NULL; uint32_t hypo_cnt_fake = hypotheses; #ifdef SSE // must be x*4 hypotheses hypo_cnt_fake += 3; hypo_cnt_fake &= ~3; printf("DPA init - using SSE (%u (+%u fake) hypotheses, tracelen %u)\n", hypotheses, hypo_cnt_fake - hypotheses, tracelen); #else printf("DPA init (%u hypotheses, tracelen %u)\n", hypotheses, tracelen); #endif memsize = (2 * tracelen); // sum_ysq & mean_y memsize += (2 * hypotheses); // sum_xsq & mean_x memsize += hypo_cnt_fake; // delta_x memsize += (1 * hypo_cnt_fake * tracelen); // sum_cross memsize *= sizeof(correl_t); memsize += sizeof(dpa_t); memsize += ((ALIGN - 1) * 2); // I don't care if we won't use SSE at all! printf("memory usage: %zd MByte\n", memsize / (1024 * 1024)); assert((dpa = malloc(memsize))); dpa->hypo_cnt = hypotheses; dpa->hypo_cnt_fake = hypo_cnt_fake; dpa->tracelen = tracelen; dpa->traces = 0; ptr = (correl_t *) (dpa + 1); bzero(ptr, hypotheses * sizeof(correl_t)); dpa->sum_xsq = ptr; ptr += hypotheses; // initialized with 1st trace dpa->mean_x = ptr; ptr += hypotheses; // align for vector unit ALIGN_PTR(ptr); // no need to bzero this. dpa->delta_x = ptr; ptr += hypo_cnt_fake; bzero(ptr, tracelen * sizeof(correl_t)); dpa->sum_ysq = ptr; ptr += tracelen; // initialized with 1st trace dpa->mean_y = ptr; ptr += tracelen; // heavy vector stuff ahead! ALIGN_PTR(ptr); bzero(ptr, hypo_cnt_fake * tracelen * sizeof(correl_t)); dpa->sum_cross = ptr; return dpa; }
/********************************************************************* * _aligned_offset_realloc (MSVCRT.@) */ void * CDECL _aligned_offset_realloc(void *memblock, MSVCRT_size_t size, MSVCRT_size_t alignment, MSVCRT_size_t offset) { void * temp, **saved; MSVCRT_size_t old_padding, new_padding, old_size; TRACE("(%p, %lu, %lu, %lu)\n", memblock, size, alignment, offset); if (!memblock) return _aligned_offset_malloc(size, alignment, offset); /* alignment must be a power of 2 */ if ((alignment & (alignment - 1)) != 0) { *MSVCRT__errno() = MSVCRT_EINVAL; return NULL; } /* offset must be less than size */ if (offset >= size) { *MSVCRT__errno() = MSVCRT_EINVAL; return NULL; } if (size == 0) { _aligned_free(memblock); return NULL; } /* don't align to less than void pointer size */ if (alignment < sizeof(void *)) alignment = sizeof(void *); /* make sure alignment and offset didn't change */ saved = SAVED_PTR(memblock); if (memblock != ALIGN_PTR(*saved, alignment, offset)) { *MSVCRT__errno() = MSVCRT_EINVAL; return NULL; } old_padding = (char *)memblock - (char *)*saved; /* Get previous size of block */ old_size = _msize(*saved); if (old_size == -1) { /* It seems this function was called with an invalid pointer. Bail out. */ return NULL; } /* Adjust old_size to get amount of actual data in old block. */ if (old_size < old_padding) { /* Shouldn't happen. Something's weird, so bail out. */ return NULL; } old_size -= old_padding; temp = MSVCRT_realloc(*saved, size + alignment + sizeof(void *)); if (!temp) return NULL; /* adjust pointer for proper alignment and offset */ memblock = ALIGN_PTR(temp, alignment, offset); /* Save the real allocation address below returned address */ /* so it can be found later to free. */ saved = SAVED_PTR(memblock); new_padding = (char *)memblock - (char *)temp; /* Memory layout of old block is as follows: +-------+---------------------+-+--------------------------+-----------+ | ... | "old_padding" bytes | | ... "old_size" bytes ... | ... | +-------+---------------------+-+--------------------------+-----------+ ^ ^ ^ | | | *saved saved memblock Memory layout of new block is as follows: +-------+-----------------------------+-+----------------------+-------+ | ... | "new_padding" bytes | | ... "size" bytes ... | ... | +-------+-----------------------------+-+----------------------+-------+ ^ ^ ^ | | | temp saved memblock However, in the new block, actual data is still written as follows (because it was copied by MSVCRT_realloc): +-------+---------------------+--------------------------------+-------+ | ... | "old_padding" bytes | ... "old_size" bytes ... | ... | +-------+---------------------+--------------------------------+-------+ ^ ^ ^ | | | temp saved memblock Therefore, min(old_size,size) bytes of actual data have to be moved from the offset they were at in the old block (temp + old_padding), to the offset they have to be in the new block (temp + new_padding == memblock). */ if (new_padding != old_padding) memmove((char *)memblock, (char *)temp + old_padding, (old_size < size) ? old_size : size); *saved = temp; return memblock; }