Exemplo n.º 1
0
/** @memo   Allocate memory.

    @doc    Allocates, if possible, a portion of memory of
    'size' bytes.

    @precondition <b>Only use if dynamic memory is required. MUST</b>
    not be called directly. Use the <b>rtp_malloc</b> macro defined
    in rtpmem.h and turn on RTP_TRACK_LOCAL_MEMORY. If the preprocessor
    macros used are unavailable in the target environment, define
    them at the compiler to a chosen dummy value.

    @return Pointer to the location of the allocated spcae,
    0 otherwise. For debugging purposes; if the cause of the
    error is obtainable at the native Memory System layer,
    turn on RTP_DEBUG in rtpmem.c to display the native
    error value.
 */
void * _rtp_debug_malloc (
  unsigned long size,                   /** Number of bytes to allocate. */
  RTP_MALLOC_FN allocFn,                /** Memory allocation function pointer. */
  const char *file,                     /** The preprocessor macro __FILE__. */
  long line_num,                         /** The preprocessor macro __LINE__. */
  const char *comment,
  unsigned long flags
  )
{
	void * ptr;

	if (!size)
	{
		return ((void *)0);
	}

  #if (RTP_MEM_RESTRAIN)
	/* ----------------------------------- */
	/*  when we have more than giMemBase   */
	/*  used, randomly deny malloc         */
	/*  requests for torture purposes      */
	/* ----------------------------------- */
	if ((size + (unsigned long) giMemUsed > giMemLimit) ||
	    ((giMemUsed > (long) giMemBase) && (rtp_rand() % 1000 < gcMallocFailRate)))
	{
		giMemClipped++;
		return ((void *)0);
	}
  #endif

	ptr = allocFn (size +
	               RTP_MEM_BUFFER_GUARD +
	               RTP_MEM_BUFFER_PREPAD +
	               sizeof(RTPMemBlockHeader));

	if (ptr)
	{
		_rtp_AddMemBlock (ptr, size, file, line_num, comment, flags);
		ptr = (void *)(((unsigned char *)ptr) + sizeof(RTPMemBlockHeader));
	}

	if (!ptr)
	{
		return ((void *)0);
	}

  #if (RTP_MEM_BUFFER_PREPAD > 0)
	rtp_memset (ptr, RTP_MEM_PREPAD_BYTE, RTP_MEM_BUFFER_PREPAD);
	ptr = (void *)(((unsigned char *)ptr) + RTP_MEM_BUFFER_PREPAD);
  #endif

  #if (RTP_MEM_BUFFER_GAURD > 0)
	rtp_memset ((void *)(((unsigned char *)ptr) + size), RTP_MEM_GUARD_BYTE, RTP_MEM_BUFFER_GUARD);
  #endif

	return (ptr);
}
Exemplo n.º 2
0
int GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
    int i;
    rtp_srand(rtp_get_system_msec());

    for (i = 0; i < sz; i++ ) {
        output[i] = rtp_rand() % 256;
        if ( (i % 8) == 7)
            rtp_srand(rtp_get_system_msec());
    }

    return 0;
}
Exemplo n.º 3
0
/** @memo   Reallocate memory.

    @doc    Allocates, if possible, a portion of memory of 'size'
    bytes, then moves 'size' bytes, if possible, of the old region
    ptr was pointing to, to the newly allocated space.

    @precondition <b>Only use if dynamic memory is required. MUST</b>
    not be called directly. Use the <b>rtp_realloc</b> macro defined
    in rtpmem.h and turn on RTP_TRACK_LOCAL_MEMORY. If the preprocessor
    macros used are unavailable in the target environment, define
    them at the compiler to a chosen dummy value.

    @return Pointer to the location of the newly allocated spcae,
    0 otherwise. For debugging purposes; if the cause of the
    error is obtainable at the native Memory System layer,
    turn on RTP_DEBUG in rtpmem.c to display the native
    error value.
 */
void * _rtp_debug_realloc (
  void * ptr,                           /** Pointer to currently allocated space. */
  unsigned long size,                   /** New number of bytes to allocate. */
  RTP_REALLOC_FN reallocFn,             /** Memory reallocation function pointer. */
  const char *file,                     /** The preprocessor macro __FILE__. */
  long line_num                         /** The preprocessor macro __LINE__. */
  )
{
void * result;
unsigned long oldSize=0;

    if (size <= 0)
    {
        return ((void *)0);
    }

    if (ptr)
    {
        ptr = ((char *)ptr) - sizeof(RTPMemBlockHeader);
        oldSize = ((RTPMemBlockHeader *)ptr)->size;
        _rtp_FreeMemBlock(ptr, file, line_num);
    }

  #if (RTP_MEM_RESTRAIN)
    /* ----------------------------------- */
	/*  when we have more than giMemBase   */
	/*  used, randomly deny malloc         */
	/*  requests for torture purposes      */
	/* ----------------------------------- */
	if (size > oldSize)
	{
    	if (((size - oldSize + (unsigned long) giMemUsed) > giMemLimit) ||
		    ((giMemUsed > (long) giMemBase) && (rtp_rand() % 100 < gcMallocFailRate)))
    	{
    		giMemClipped++;
    		_rtp_AddMemBlock(ptr, oldSize, file, line_num, "Realloc", 0);
    		return ((void *)0);
    	}
    }
  #endif

    result = reallocFn(ptr, size +
                       RTP_MEM_BUFFER_GUARD +
                       RTP_MEM_BUFFER_PREPAD +
                       sizeof(RTPMemBlockHeader));

    if (result)
    {
        _rtp_AddMemBlock(result, size, file, line_num,"Realloc", 0);
        result = (void *)((char *)result + sizeof(RTPMemBlockHeader));

	  #if (RTP_MEM_BUFFER_PREPAD > 0)
        rtp_memset (ptr, RTP_MEM_PREPAD_BYTE, RTP_MEM_BUFFER_PREPAD);
        ptr = (void *)(((unsigned char *)ptr) + RTP_MEM_BUFFER_PREPAD);
	  #endif

	  #if (RTP_MEM_BUFFER_GUARD > 0)
        rtp_memset ((void *)(((unsigned char *)ptr) + size), RTP_MEM_GUARD_BYTE, RTP_MEM_BUFFER_GUARD);
	  #endif
    }
    else
    {
        if (ptr)
        {
            _rtp_AddMemBlock(ptr, oldSize, file, line_num,"Realloc", 0);
        }
    }

    return (result);
}