示例#1
0
/*
    memory_tracker_dump()
    Dumps the current contents of the global memory allocation list
*/
static void memory_tracker_dump() {
  int i = 0;
  struct mem_block *p = (memtrack.head ? memtrack.head->next : NULL);

  memtrack_log("\n_currently Allocated= %d; Max allocated= %d\n",
               memtrack.current_allocated, memtrack.max_allocated);

  while (p) {
#if defined(WIN32) && !defined(_WIN32_WCE)

    /*when using outputdebugstring, output filenames so they
      can be clicked to be opened in visual studio*/
    if (g_logging.type == 1)
      memtrack_log("memblocks[%d].addr= 0x%.8x, memblocks[%d].size= %d, file:\n"
                   "  %s(%d):\n", i,
                   p->addr, i, p->size,
                   p->file, p->line);
    else
#endif
      memtrack_log("memblocks[%d].addr= 0x%.8x, memblocks[%d].size= %d, file: %s, line: %d\n", i,
                   p->addr, i, p->size,
                   p->file, p->line);

    p = p->next;
    ++i;
  }

  memtrack_log("\n");
}
static void memory_tracker_dump() {
  int i = 0;
  struct mem_block *p = (memtrack.head ? memtrack.head->next : NULL);

  memtrack_log("\n_currently Allocated= %d; Max allocated= %d\n",
               memtrack.current_allocated, memtrack.max_allocated);

  while (p) {
#if defined(WIN32) && !defined(_WIN32_WCE)

    if (g_logging.type == 1)
      memtrack_log("memblocks[%d].addr= 0x%.8x, memblocks[%d].size= %d, file:\n"
                   "  %s(%d):\n", i,
                   p->addr, i, p->size,
                   p->file, p->line);
    else
#endif
      memtrack_log("memblocks[%d].addr= 0x%.8x, memblocks[%d].size= %d, file: %s, line: %d\n", i,
                   p->addr, i, p->size,
                   p->file, p->line);

    p = p->next;
    ++i;
  }

  memtrack_log("\n");
}
示例#3
0
/*
    vpx_memory_tracker_init(int padding_size, int pad_value)
      padding_size - the size of the padding before and after each mem addr.
                     Values > 0 indicate that integrity checks can be performed
                     by inspecting these areas.
      pad_value - the initial value within the padding area before and after
                  each mem addr.

    Initializes global memory tracker structure
    Allocates the head of the list
*/
int vpx_memory_tracker_init(int padding_size, int pad_value)
{
    if (!g_b_mem_tracker_inited)
    {
        if (memtrack.head = (struct mem_block *)MEM_TRACK_MALLOC(sizeof(struct mem_block)))
        {
            int ret;

            MEM_TRACK_MEMSET(memtrack.head, 0, sizeof(struct mem_block));

            memtrack.tail = memtrack.head;

            memtrack.current_allocated = 0;
            memtrack.max_allocated     = 0;

            memtrack.padding_size = padding_size;
            memtrack.pad_value    = pad_value;

#if defined(LINUX) || defined(__uClinux__)
            ret = pthread_mutex_init(&memtrack.mutex,
                                     NULL);            /*mutex attributes (NULL=default)*/
#elif defined(WIN32) || defined(_WIN32_WCE)
            memtrack.mutex = create_mutex(NULL,   /*security attributes*/
                                          FALSE,  /*we don't want initial ownership*/
                                          NULL);  /*mutex name*/
            ret = !memtrack.mutex;
#elif defined(VXWORKS)
            memtrack.mutex = sem_bcreate(SEM_Q_FIFO, /*SEM_Q_FIFO non-priority based mutex*/
                                         SEM_FULL);  /*SEM_FULL initial state is unlocked*/
            ret = !memtrack.mutex;
#elif defined(NDS_NITRO)
            os_init_mutex(&memtrack.mutex);
            ret = 0;
#elif defined(NO_MUTEX)
            ret = 0;
#endif

            if (ret)
            {
                memtrack_log("vpx_memory_tracker_init: Error creating mutex!\n");

                MEM_TRACK_FREE(memtrack.head);
                memtrack.head = NULL;
            }
            else
            {
                memtrack_log("Memory Tracker init'd, v."vpx_mem_tracker_version" pad_size:%d pad_val:0x%x %d\n"
                             , padding_size
                             , pad_value
                             , pad_value);
                g_b_mem_tracker_inited = 1;
            }
        }
    }

    return g_b_mem_tracker_inited;
}
int vpx_memory_tracker_init(int padding_size, int pad_value) {
  if (!g_b_mem_tracker_inited) {
    if ((memtrack.head = (struct mem_block *)
                         MEM_TRACK_MALLOC(sizeof(struct mem_block)))) {
      int ret;

      MEM_TRACK_MEMSET(memtrack.head, 0, sizeof(struct mem_block));

      memtrack.tail = memtrack.head;

      memtrack.current_allocated = 0;
      memtrack.max_allocated     = 0;

      memtrack.padding_size = padding_size;
      memtrack.pad_value    = pad_value;

#if HAVE_PTHREAD_H
      ret = pthread_mutex_init(&memtrack.mutex,
                               NULL);            
#elif defined(WIN32) || defined(_WIN32_WCE)
      memtrack.mutex = CreateMutex(NULL,   
                                   FALSE,  
                                   NULL);  
      ret = !memtrack.mutex;
#elif defined(VXWORKS)
      memtrack.mutex = sem_bcreate(SEM_Q_FIFO, 
                                   SEM_FULL);  
      ret = !memtrack.mutex;
#elif defined(NO_MUTEX)
      ret = 0;
#endif

      if (ret) {
        memtrack_log("vpx_memory_tracker_init: Error creating mutex!\n");

        MEM_TRACK_FREE(memtrack.head);
        memtrack.head = NULL;
      } else {
        memtrack_log("Memory Tracker init'd, v."vpx_mem_tracker_version" pad_size:%d pad_val:0x%x %d\n"
, padding_size
, pad_value
, pad_value);
        g_b_mem_tracker_inited = 1;
      }
    }
  }

  return g_b_mem_tracker_inited;
}
示例#5
0
/*
    memory_tracker_remove(size_t addr)
    Removes an address and its corresponding size (if they exist)
    from the memory tracker list and adjusts the current number
    of bytes allocated.
    Return:
      0: on success
      -1: if the mutex could not be locked
      -2: if the addr was not found in the list
*/
int memory_tracker_remove(size_t addr) {
  int ret = -1;

  if (!memory_tracker_lock_mutex()) {
    struct mem_block *p;

    if ((p = memory_tracker_find(addr))) {
      memtrack.current_allocated -= p->size;

      p->prev->next = p->next;

      if (p->next)
        p->next->prev = p->prev;
      else
        memtrack.tail = p->prev;

      ret = 0;
      MEM_TRACK_FREE(p);
    } else {
      if (addr)
        memtrack_log("memory_tracker_remove(): addr not found in list,"
                     " 0x%.8x\n", addr);

      ret = -2;
    }

    memory_tracker_unlock_mutex();
  }

  return ret;
}
示例#6
0
/*
    memory_tracker_add(size_t addr, unsigned int size,
                     char * file, unsigned int line)
    Adds an address (addr), it's size, file and line number to our list.
    Adjusts the total bytes allocated and max bytes allocated if necessary.
    If memory cannot be allocated the list will be destroyed.
*/
void memory_tracker_add(size_t addr, unsigned int size,
                        char *file, unsigned int line,
                        int padded) {
  if (!memory_tracker_lock_mutex()) {
    struct mem_block *p;

    p = MEM_TRACK_MALLOC(sizeof(struct mem_block));

    if (p) {
      p->prev       = memtrack.tail;
      p->prev->next = p;
      p->addr       = addr;
      p->size       = size;
      p->line       = line;
      p->file       = file;
      p->padded     = padded;
      p->next       = NULL;

      memtrack.tail = p;

      memtrack.current_allocated += size;

      if (memtrack.current_allocated > memtrack.max_allocated)
        memtrack.max_allocated = memtrack.current_allocated;

      // memtrack_log("memory_tracker_add: added addr=0x%.8x\n", addr);

      memory_tracker_unlock_mutex();
    } else {
      memtrack_log("memory_tracker_add: error allocating memory!\n");
      memory_tracker_unlock_mutex();
      vpx_memory_tracker_destroy();
    }
  }
}
示例#7
0
/*
    vpx_memory_tracker_set_log_type
    Sets the logging type for the memory tracker. Based on the value it will
    direct its output to the appropriate place.
    Return:
      0: on success
      -1: if the logging type could not be set, because the value was invalid
          or because a file could not be opened
*/
int vpx_memory_tracker_set_log_type(int type, char *option) {
  int ret = -1;

  switch (type) {
    case 0:
      g_logging.type = 0;

      if (!option) {
        g_logging.file = stderr;
        ret = 0;
      } else {
        if ((g_logging.file = fopen((char *)option, "w")))
          ret = 0;
      }

      break;
#if defined(WIN32) && !defined(_WIN32_WCE)
    case 1:
      g_logging.type = type;
      ret = 0;
      break;
#endif
    default:
      break;
  }

  // output the version to the new logging destination
  if (!ret)
    memtrack_log("Memory Tracker logging initialized, "
                 "Memory Tracker v."vpx_mem_tracker_version"\n");

  return ret;
}
示例#8
0
/*
    memory_tracker_unlock_mutex()
    Unlocks the memory tracker mutex with a platform specific call
    Returns:
        0: Success
       <0: Failure, either the mutex was not initialized
           or the call to unlock the mutex failed
*/
static int memory_tracker_unlock_mutex()
{
    int ret = -1;

    if (g_b_mem_tracker_inited)
    {

#if defined(LINUX) || defined(__uClinux__)
        ret = pthread_mutex_unlock(&memtrack.mutex);
#elif defined(WIN32) || defined(_WIN32_WCE)
        ret = !release_mutex(memtrack.mutex);
#elif defined(VXWORKS)
        ret = sem_give(memtrack.mutex);
#elif defined(NDS_NITRO)
        os_unlock_mutex(&memtrack.mutex);
        ret = 0;
#endif

        if (ret)
        {
            memtrack_log("memory_tracker_unlock_mutex: mutex unlock failed\n");
        }
    }

    return ret;
}
示例#9
0
/*
    memory_tracker_lock_mutex()
    Locks the memory tracker mutex with a platform specific call
    Returns:
        0: Success
       <0: Failure, either the mutex was not initialized
           or the call to lock the mutex failed
*/
static int memory_tracker_lock_mutex()
{
    int ret = -1;

    if (g_b_mem_tracker_inited)
    {

#if defined(LINUX) || defined(__uClinux__)
        ret = pthread_mutex_lock(&memtrack.mutex);
#elif defined(WIN32) || defined(_WIN32_WCE)
        ret = WaitForSingleObject(memtrack.mutex, INFINITE);
#elif defined(VXWORKS)
        ret = sem_take(memtrack.mutex, WAIT_FOREVER);
#elif defined(NDS_NITRO)
        os_lock_mutex(&memtrack.mutex);
        ret = 0;
#endif

        if (ret)
        {
            memtrack_log("memory_tracker_lock_mutex: mutex lock failed\n");
        }
    }

    return ret;
}
示例#10
0
/*
    memory_tracker_check_integrity(char* file, unsigned int file)
      file - the file name where the check was placed
      line - the line in file where the check was placed
    If a padding_size was supplied to vpx_memory_tracker_init()
    this function will check ea. addr in the list verifying that
    addr-padding_size and addr+padding_size is filled with pad_value
*/
static void memory_tracker_check_integrity(char *file, unsigned int line)
{
    if (memtrack.padding_size)
    {
        int i,
            index = 0;
        unsigned char *p_show_me,
                 * p_show_me2;
        unsigned int tempme = memtrack.pad_value,
                     dead1,
                     dead2;
        unsigned char *x_bounds;
        struct mem_block *p = memtrack.head->next;

        while (p)
        {
            //x_bounds = (unsigned char*)p->addr;
            //back up VPX_BYTE_ALIGNMENT
            //x_bounds -= memtrack.padding_size;

            if (p->padded)   // can the bounds be checked?
            {
                /*yes, move to the address that was actually allocated
                by the vpx_* calls*/
                x_bounds = (unsigned char *)(((size_t *)p->addr)[-1]);

                for (i = 0; i < memtrack.padding_size; i += sizeof(unsigned int))
                {
                    p_show_me = (x_bounds + i);
                    p_show_me2 = (unsigned char *)(p->addr + p->size + i);

                    MEM_TRACK_MEMCPY(&dead1, p_show_me, sizeof(unsigned int));
                    MEM_TRACK_MEMCPY(&dead2, p_show_me2, sizeof(unsigned int));

                    if ((dead1 != tempme) || (dead2 != tempme))
                    {
                        memtrack_log("\n[vpx_mem integrity check failed]:\n"
                                     "    index[%d] {%s:%d} addr=0x%x, size=%d,"
                                     " file: %s, line: %d c0:0x%x c1:0x%x\n",
                                     index, file, line, p->addr, p->size, p->file,
                                     p->line, dead1, dead2);
                    }
                }
            }

            ++index;
            p = p->next;
        }
    }
}
示例#11
0
/*
    vpx_memory_tracker_set_log_func
    Sets a logging function to be used by the memory tracker.
    Return:
      0: on success
      -1: if the logging type could not be set because logfunc was NULL
*/
int vpx_memory_tracker_set_log_func(void *userdata,
                                    void(*logfunc)(void *userdata,
                                                   const char *fmt, va_list args)) {
  int ret = -1;

  if (logfunc) {
    g_logging.type     = -1;
    g_logging.userdata = userdata;
    g_logging.func     = logfunc;
    ret = 0;
  }

  // output the version to the new logging destination
  if (!ret)
    memtrack_log("Memory Tracker logging initialized, "
                 "Memory Tracker v."vpx_mem_tracker_version"\n");

  return ret;
}
static void memory_tracker_check_integrity(char *file, unsigned int line) {
  if (memtrack.padding_size) {
    int i,
        index = 0;
    unsigned char *p_show_me,
             * p_show_me2;
    unsigned int tempme = memtrack.pad_value,
                 dead1,
                 dead2;
    unsigned char *x_bounds;
    struct mem_block *p = memtrack.head->next;

    while (p) {
      
      
      

      if (p->padded) { 
        x_bounds = (unsigned char *)(((size_t *)p->addr)[-1]);

        for (i = 0; i < memtrack.padding_size; i += sizeof(unsigned int)) {
          p_show_me = (x_bounds + i);
          p_show_me2 = (unsigned char *)(p->addr + p->size + i);

          MEM_TRACK_MEMCPY(&dead1, p_show_me, sizeof(unsigned int));
          MEM_TRACK_MEMCPY(&dead2, p_show_me2, sizeof(unsigned int));

          if ((dead1 != tempme) || (dead2 != tempme)) {
            memtrack_log("\n[vpx_mem integrity check failed]:\n"
                         "    index[%d,%d] {%s:%d} addr=0x%x, size=%d,"
                         " file: %s, line: %d c0:0x%x c1:0x%x\n",
                         index, i, file, line, p->addr, p->size, p->file,
                         p->line, dead1, dead2);
          }
        }
      }

      ++index;
      p = p->next;
    }
  }
}
示例#13
0
/*
    memory_tracker_unlock_mutex()
    Unlocks the memory tracker mutex with a platform specific call
    Returns:
        0: Success
       <0: Failure, either the mutex was not initialized
           or the call to unlock the mutex failed
*/
static int memory_tracker_unlock_mutex() {
  int ret = -1;

  if (g_b_mem_tracker_inited) {

#if HAVE_PTHREAD_H
    ret = pthread_mutex_unlock(&memtrack.mutex);
#elif defined(WIN32) || defined(_WIN32_WCE)
    ret = !ReleaseMutex(memtrack.mutex);
#elif defined(VXWORKS)
    ret = sem_give(memtrack.mutex);
#endif

    if (ret) {
      memtrack_log("memory_tracker_unlock_mutex: mutex unlock failed\n");
    }
  }

  return ret;
}