int AFX_CDECL AfxCriticalNewHandler(size_t nSize) // nSize is already rounded { // called during critical memory allocation // free up part of the app's safety cache TRACE0("Warning: Critical memory allocation failed!\n"); _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); if (pThreadState != NULL && pThreadState->m_pSafetyPoolBuffer != NULL) { size_t nOldBufferSize = _msize(pThreadState->m_pSafetyPoolBuffer); if (nOldBufferSize <= nSize + MIN_MALLOC_OVERHEAD) { // give it all up TRACE0("Warning: Freeing application's memory safety pool!\n"); free(pThreadState->m_pSafetyPoolBuffer); pThreadState->m_pSafetyPoolBuffer = NULL; } else { BOOL bEnable = AfxEnableMemoryTracking(FALSE); _expand(pThreadState->m_pSafetyPoolBuffer, nOldBufferSize - (nSize + MIN_MALLOC_OVERHEAD)); AfxEnableMemoryTracking(bEnable); TRACE3("Warning: Shrinking safety pool from %d to %d to satisfy request of %d bytes.\n", nOldBufferSize, _msize(pThreadState->m_pSafetyPoolBuffer), nSize); } return 1; // retry it } TRACE0("ERROR: Critical memory allocation from safety pool failed!\n"); AfxThrowMemoryException(); // oops return 0; }
int XTree::addText(int eid, int lsid, std::string text, unsigned long long value) { _elementIndex++; int topid = _elementIndex / _botCap; int botid = _elementIndex % _botCap; if (botid == 0) _expand(topid); int etopid = eid / _botCap; int ebotid = eid % _botCap; if (lsid == NULL_NODE) _firstChild[etopid][ebotid] = _elementIndex; else _nextSibling[lsid/_botCap][lsid%_botCap] = _elementIndex; _childrenCount[etopid][ebotid]++; _hashValue[topid][botid] = value; _valueCount++; int vtopid = _valueCount / _botCap; int vbotid = _valueCount % _botCap; if (vbotid == 0) _value[vtopid] = new std::string[_botCap]; _value[vtopid][vbotid] = text; _valueIndex[topid][botid] = _valueCount; return _elementIndex; }
int32_t _read_withsize( struct buffer * self, int32_t fd, int32_t nbytes ) { int32_t nread = -1; if ( nbytes == -1 ) { int32_t rc = ioctl( fd, FIONREAD, &nread ); if ( rc == -1 || nread == 0 ) { nbytes = RECV_BUFFER_SIZE; } else { nbytes = nread; } } if ( _expand( self, nbytes ) != 0 ) { return -2; } nread = (int32_t)read( fd, self->buffer+self->length, nbytes ); if ( nread > 0 ) { self->length += nread; } return nread; }
void *CStdMemAlloc::Expand( void *pMem, size_t nSize, const char *pFileName, int nLine ) { #ifdef _WIN32 return _expand( pMem, nSize ); #elif _LINUX return realloc( pMem, nSize ); #endif }
void *CStdMemAlloc::Expand( void *pMem, size_t nSize ) { #ifdef _WIN32 return _expand( pMem, nSize ); #elif _LINUX return realloc( pMem, nSize ); #endif }
void* _remove_from_list() { if (_m_head->m_next == 0) _expand(); sp_node<__Type>* _Node = _m_head; _m_head = _Node->m_next; return _Node; }
static void* lzham_default_realloc(void* p, size_t size, size_t* pActual_size, lzham_bool movable, void* pUser_data) { LZHAM_NOTE_UNUSED(pUser_data); void* p_new; if (!p) { p_new = malloc(size); LZHAM_ASSERT( (reinterpret_cast<ptr_bits_t>(p_new) & (LZHAM_MIN_ALLOC_ALIGNMENT - 1)) == 0 ); if (pActual_size) *pActual_size = p_new ? _msize(p_new) : 0; } else if (!size) { free(p); p_new = NULL; if (pActual_size) *pActual_size = 0; } else { void* p_final_block = p; #ifdef WIN32 p_new = _expand(p, size); #else p_new = NULL; #endif if (p_new) { LZHAM_ASSERT( (reinterpret_cast<ptr_bits_t>(p_new) & (LZHAM_MIN_ALLOC_ALIGNMENT - 1)) == 0 ); p_final_block = p_new; } else if (movable) { p_new = realloc(p, size); if (p_new) { LZHAM_ASSERT( (reinterpret_cast<ptr_bits_t>(p_new) & (LZHAM_MIN_ALLOC_ALIGNMENT - 1)) == 0 ); p_final_block = p_new; } } if (pActual_size) *pActual_size = _msize(p_final_block); } return p_new; }
void *CDbgMemAlloc::Expand( void *pMem, size_t nSize ) { // FIXME: Should these gather stats? // return Expand( pMem, nSize, pUnknown, 0 ); // void * ptr = (char*)pMem - sizeof( size_t ); // size_t nNewSize = nSize + sizeof( size_t ); // size_t nOldSize = *(size_t*)ptr; // ptr = MemoryAllocator( ).reallocate( ptr, nOldSize, nNewSize ); // *(size_t*)ptr = nNewSize; // return (char*)ptr + sizeof( size_t ); return _expand( pMem, nSize ); }
// Expand. Like expand(), but expand entire subtree void DispValue::expandAll(int depth) { if (depth == 0) return; _expand(); for (int i = 0; i < nchildren(); i++) { child(i)->expandAll(depth - 1); } }
static int _keyIndex(xdict *d, const void *key) { unsigned int h; xdictEntry *he; if (_expand(d) == XDICT_ERR) return -1; h = xdictHashKey(d, key) & d->sizemask; he = d->table[h]; while (he) { if (xdictCompareHashKeys(d, key, he->key)) return -1; he = he->next; } return h; }
void *LnkExpand( void *src, size_t size ) /***************************************/ // try to expand a block of memory { #ifdef _ZDOS return ( NULL ); #else #ifdef TRMEM return( _trmem_expand( src, size, _trmem_guess_who(), TrHdl ) ); #else return( _expand( src, size ) ); #endif #endif }
bool parcHashCodeTable_Add(PARCHashCodeTable *table, void *key, void *data) { assertNotNull(table, "Parameter table must be non-null"); assertNotNull(key, "Parameter key must be non-null"); assertNotNull(data, "Parameter data must be non-null"); if (table->hashtable.tableSize >= table->hashtable.expandThreshold) { _expand(table); } HashCodeType hashcode = table->keyHashCodeFunc(key); PARCHashCodeTable_AddResult result = ADD_OK; do { result = _innerTableAdd(&table->hashtable, table->keyEqualsFunc, hashcode, key, data); if (result == ADD_NOSPACE) { _expand(table); } } while (result == ADD_NOSPACE); return (result == ADD_OK); }
size_t my_string_set(my_string_t *obj, const char* str) { size_t len = my_strlen(str); PTR_CHECK_RETURN(obj, "my_string", 0); if ((len+1) > obj->mem_size) { _clear(obj); _expand(obj, len*2); } _set(obj, str); signal_emit(obj->update_signal); return obj->str_len; }
int32_t buffer_append( struct buffer * self, char * buf, uint32_t length ) { uint32_t offset = _offset(self); uint32_t needlength = offset + self->length + length; if ( needlength > self->capacity ) { if ( _expand(self, length) == -1 ) { return -1; } } memcpy( self->buffer+self->length, buf, length ); self->length += length; return 0; }
void main() { char *buf; char __far *buf2; buf = (char *) malloc( 80 ); printf( "Size of buffer is %u\n", _msize(buf) ); if( _expand( buf, 100 ) == NULL ) { printf( "Unable to expand buffer\n" ); } printf( "New size of buffer is %u\n", _msize(buf) ); buf2 = (char __far *) _fmalloc( 2000 ); printf( "Size of far buffer is %u\n", _fmsize(buf2) ); if( _fexpand( buf2, 8000 ) == NULL ) { printf( "Unable to expand far buffer\n" ); } printf( "New size of far buffer is %u\n", _fmsize(buf2) ); }
static void _add(struct connection_server * server, int fd , uint32_t address) { ++server->current_connection; if (server->current_connection > server->max_connection) { _expand(server); } int i; for (i=0;i<server->max_connection;i++) { struct connection * c = &server->conn[i]; if (c->address == 0) { c->fd = fd; c->address = address; c->close = 0; int err = connection_add(server->pool, fd , c); assert(err == 0); return; } } assert(0); }
int XTree::addElement(int pid, int lsid, std::string tagName) { _elementIndex++; int topid = _elementIndex / _botCap; int botid = _elementIndex % _botCap; if (botid == 0) _expand(topid); // Check if we've got the element name hash_map <std::string, int, HashString>::const_iterator hit = _tagNames.find(tagName); if (hit != _tagNames.end()) { int id = hit->second; _valueIndex[topid][botid] = id; } else { _tagIndex++; _value[0][_tagIndex] = tagName; _tagNames[tagName] = _tagIndex; _valueIndex[topid][botid] = _tagIndex; } if (pid == NULL_NODE) return _elementIndex; int ptopid = pid / _botCap; int pbotid = pid % _botCap; // parent-child relation or sibling-sibling ralation if (lsid == NULL_NODE) _firstChild[ptopid][pbotid] = _elementIndex; else _nextSibling[lsid/_botCap][lsid%_botCap] = _elementIndex; // update children count _childrenCount[ptopid][pbotid]++; return _elementIndex; }
unsigned khlist_set(struct khlist *hl, const char *str, void *ud){ unsigned hash = _hash_string(str, HASH_OFFSET); unsigned hasha = _hash_string(str, HASH_A); unsigned hashb = _hash_string(str, HASH_B); unsigned hash_start = hash % hl->size; unsigned npos = hash_start; while (hl->hash_t[npos].exist == 1){ if (hl->hash_t[npos].hasha == hasha && hl->hash_t[npos].hashb == hashb) return -1; npos = (npos + 1) % hl->size; if (npos == hash_start){ if (_expand(hl) == NULL) return -1; return khlist_set(hl, str, ud); } } hl->hash_t[npos].exist = 1; hl->hash_t[npos].hasha = hasha; hl->hash_t[npos].hashb = hashb; hl->hash_t[npos].ud = ud; return npos; }
free_list(std::size_t _Size = 1024) { _m_head = 0; _expand(_Size); }
bool AStarPlanner::computePath(std::vector<Util::Point>& agent_path, Util::Point start, Util::Point goal, SteerLib::GridDatabase2D * _gSpatialDatabase, bool append_to_path) { gSpatialDatabase = _gSpatialDatabase; int startIndex = gSpatialDatabase->getCellIndexFromLocation(start); SearchNodePtr startNode(new SearchNode(startIndex, 0, 0)); int goalIndex = gSpatialDatabase->getCellIndexFromLocation(goal); std::vector<SearchNodePtr> open; std::vector<SearchNodePtr> closed; SearchNodePtr goalNode(nullptr); open.push_back(startNode); while (!open.empty()) { // Get the node with the minimum f, breaking ties on g. std::vector<SearchNodePtr>::iterator minIter = std::min_element(open.begin(), open.end()); SearchNodePtr minNode = *minIter; open.erase(minIter); // If we're visiting the goal, we're finished. if (minNode->index() == goalIndex) { goalNode = minNode; break; } // Expand this node. std::vector<SearchNodePtr> expandedList = _expand(minNode, goal); for (SearchNodePtr& expandedNode : expandedList) { // If this cell is already in the open set, check if this is a cheaper path. std::vector<SearchNodePtr>::iterator iter = std::find(open.begin(), open.end(), expandedNode); if (iter != open.end()) { if (expandedNode->g() < (*iter)->g()) { (*iter)->g(expandedNode->g()); (*iter)->prev(minNode); } } else { if (std::find(closed.begin(), closed.end(), expandedNode) == closed.end()) { expandedNode->prev(minNode); open.push_back(expandedNode); } } } closed.push_back(minNode); } // Check if a path to the goal was not found. if (goalNode == nullptr) { return false; } // Go back from goal node to start. SearchNodePtr ptr = goalNode; float totalPathCost = 0; while (ptr != nullptr) { Util::Point point; gSpatialDatabase->getLocationFromIndex(ptr->index(), point); agent_path.insert(agent_path.begin(), point); SearchNodePtr prev = ptr->prev(); if (prev == nullptr) break; Util::Point prevPoint; gSpatialDatabase->getLocationFromIndex(prev->index(), prevPoint); totalPathCost += distanceBetween(point, prevPoint); ptr = prev; } std::cout << "\nTotal path cost is " << totalPathCost << std::endl; std::cout << "\nThe number of expanded nodes is " << numExpanded << std::endl; return true; }
/* Parse a message specification from (argc;argv). Returned msgset is not sorted nor optimised */ int _mh_msgset_parse (mu_mailbox_t mbox, mh_msgset_t *msgset, int argc, char **argv) { size_t msgcnt; size_t *msglist; size_t i, msgno; if (argc == 0) return 1; msgcnt = argc; msglist = calloc (msgcnt, sizeof(*msglist)); for (i = 0, msgno = 0; i < argc; i++) { char *p = NULL, *q; size_t start, end; size_t msg_first, n; long num; char *arg = msgset_preproc (mbox, argv[i]); if (!mu_isdigit (arg[0])) { int j; mh_msgset_t m; if (expand_user_seq (mbox, &m, arg)) { mu_error (_("message set %s does not exist"), arg); exit (1); } _expand (&msgcnt, &msglist, m.count); for (j = 0; j < m.count; j++) msglist[msgno++] = m.list[j]; mh_msgset_free (&m); } else { start = strtoul (arg, &p, 0); switch (*p) { case 0: n = mh_get_message (mbox, start, NULL); if (!n) { mu_error (_("message %lu does not exist"), (unsigned long) start); exit (1); } msglist[msgno++] = n; break; case '-': end = strtoul (p+1, &p, 0); if (*p) msgset_abort (argv[i]); if (end < start) { size_t t = start; start = end; end = t; } _expand (&msgcnt, &msglist, end - start); msg_first = msgno; for (; start <= end; start++) { n = mh_get_message (mbox, start, NULL); if (n) msglist[msgno++] = n; } if (msgno == msg_first) { mu_error (_("no messages in range %s"), argv[i]); exit (1); } break; case ':': num = strtoul (p+1, &q, 0); if (*q) msgset_abort (argv[i]); if (p[1] != '+' && p[1] != '-') { if (strncmp (argv[i], "last:", 5) == 0 || strncmp (argv[i], "prev:", 5) == 0) num = -num; } end = start + num; if (end < start) { size_t t = start; start = end + 1; end = t; } else end--; _expand (&msgcnt, &msglist, end - start); msg_first = msgno; for (; start <= end; start++) { n = mh_get_message (mbox, start, NULL); if (n) msglist[msgno++] = n; } if (msgno == msg_first) { mu_error (_("no messages in range %s"), argv[i]); exit (1); } break; default: msgset_abort (argv[i]); } } free (arg); } msgset->count = msgno; msgset->list = msglist; return 0; }
// runs all of the tests in non-verbose mode by default (i.e. printing only failures) // individual groups of tests can be run by using the -t:<tests> option where <tests> // is one or more of // l testing_lookups // $ testing_$_expand // [ testing_$$_expand // e testing_$ENV_expand // F testing_$F_expand and testing_$F_regressions // c testing_$CHOICE_expand // s testing_$SUBSTR_expand // i testing_$INT_expand // r testing_$REAL_expand // n testing_$INT_expand and testing_$REAL_expand // r testing_$RAND_expand (RANDOM_INTEGER and RANDOM_CHOICE) // P testing_parser config/submit/metaknob parser. // int main( int /*argc*/, const char ** argv) { int test_flags = 0; const char * pcolon; for (int ii = 1; argv[ii]; ++ii) { const char *arg = argv[ii]; if (is_dash_arg_prefix(arg, "verbose", 1)) { dash_verbose = 1; } else if (is_dash_arg_colon_prefix(arg, "test", &pcolon, 1)) { if (pcolon) { while (*++pcolon) { switch (*pcolon) { case 'l': test_flags |= 0x0001; break; // lookup case '$': test_flags |= 0x0002; break; // $ case '[': test_flags |= 0x0004; break; // $$ case 'e': test_flags |= 0x0008; break; // $ENV case 'F': test_flags |= 0x0010 | 0x0020; break; // $F case 'c': test_flags |= 0x0040; break; // $CHOICE case 's': test_flags |= 0x0080; break; // $SUBSTR case 'i': test_flags |= 0x0100; break; // $INT case 'f': test_flags |= 0x0200; break; // $REAL case 'n': test_flags |= 0x0100 | 0x0200; break; // $INT, $REAL case 'r': test_flags |= 0x0400; break; // $RANDOM_INTEGER and $RANDOM_CHOICE case 'p': test_flags |= 0x1000; break; // parse } } } else { test_flags = 3; } } else { fprintf(stderr, "unknown argument %s\n", arg); return 1; } } if ( ! test_flags) test_flags = -1; TestingMacroSet.defaults->size = param_info_init((const void**)&TestingMacroSet.defaults->table); TestingMacroSet.options |= CONFIG_OPT_DEFAULTS_ARE_PARAM_INFO; insert_testing_macros(NULL, "TEST"); optimize_macros(TestingMacroSet); if (test_flags & 0x0001) testing_lookups(dash_verbose); if (test_flags & 0x0002) testing_$_expand(dash_verbose); if (test_flags & 0x0004) testing_$$_expand(dash_verbose); if (test_flags & 0x0008) testing_$ENV_expand(dash_verbose); if (test_flags & 0x0010) testing_$F_expand(dash_verbose); if (test_flags & 0x0020) testing_$F_regressions(dash_verbose); if (test_flags & 0x0040) testing_$CHOICE_expand(dash_verbose); if (test_flags & 0x0080) testing_$SUBSTR_expand(dash_verbose); if (test_flags & 0x0100) testing_$INT_expand(dash_verbose); if (test_flags & 0x0200) testing_$REAL_expand(dash_verbose); if (test_flags & 0x0400) testing_$RAND_expand(dash_verbose); if (test_flags & 0x1000) testing_parser(dash_verbose); return fail_count > 0; }
extern "C" void* __cdecl _aligned_offset_realloc_base( void* block, size_t size, size_t align, size_t offset ) { uintptr_t ptr, retptr, gap, stptr, diff; uintptr_t movsz, reqsz; int bFree = 0; /* special cases */ if (block == nullptr) { return _aligned_offset_malloc_base(size, align, offset); } if (size == 0) { _aligned_free_base(block); return nullptr; } /* validation section */ _VALIDATE_RETURN(IS_2_POW_N(align), EINVAL, nullptr); _VALIDATE_RETURN(offset == 0 || offset < size, EINVAL, nullptr); stptr = (uintptr_t)block; /* ptr points to the pointer to starting of the memory block */ stptr = (stptr & ~(PTR_SZ -1)) - PTR_SZ; /* ptr is the pointer to the start of memory block*/ stptr = *((uintptr_t *)stptr); align = (align > PTR_SZ ? align : PTR_SZ) -1; /* gap = number of bytes needed to round up offset to align with PTR_SZ*/ gap = (0 -offset)&(PTR_SZ -1); diff = (uintptr_t)block - stptr; /* Mov size is min of the size of data available and sizw requested. */ #pragma warning(push) #pragma warning(disable: 22018) // Silence prefast about overflow/underflow movsz = _msize((void *)stptr) - ((uintptr_t)block - stptr); #pragma warning(pop) movsz = movsz > size ? size : movsz; reqsz = PTR_SZ + gap + align + size; _VALIDATE_RETURN_NOEXC(size <= reqsz, ENOMEM, nullptr); /* First check if we can expand(reducing or expanding using expand) data * safely, ie no data is lost. eg, reducing alignment and keeping size * same might result in loss of data at the tail of data block while * expanding. * * If no, use malloc to allocate the new data and move data. * * If yes, expand and then check if we need to move the data. */ if ((stptr +align +PTR_SZ +gap)<(uintptr_t)block) { if ((ptr = (uintptr_t)malloc(reqsz)) == (uintptr_t) nullptr) return nullptr; bFree = 1; } else { /* we need to save errno, which can be modified by _expand */ errno_t save_errno = errno; if ((ptr = (uintptr_t)_expand((void *)stptr, reqsz)) == (uintptr_t)nullptr) { errno = save_errno; if ((ptr = (uintptr_t)malloc(reqsz)) == (uintptr_t) nullptr) return nullptr; bFree = 1; } else stptr = ptr; } if ( ptr == ((uintptr_t)block - diff) && !( ((size_t)block + gap +offset) & ~(align) )) { return block; } retptr =((ptr +PTR_SZ +gap +align +offset)&~align)- offset; memmove((void *)retptr, (void *)(stptr + diff), movsz); if ( bFree) free ((void *)stptr); ((uintptr_t *)(retptr - gap))[-1] = ptr; return (void *)retptr; }