Пример #1
0
fsl_osal_ptr dbg_realloc(fsl_osal_ptr ptr, fsl_osal_u32 size, fsl_osal_char * desc, fsl_osal_s32 line)
{
	Mem_Desc * bt = tm->head;
	fsl_osal_ptr buf = NULL;

	if(ptr == NULL)
		return dbg_malloc(size, desc, line);

	if(size == 0)
	{
		dbg_free(ptr);
		return NULL;
	}

	//find the mem descripter for ptr
	while(bt)
	{
		if (bt->mem==ptr)
			break;
		bt=bt->next;
	}

	buf = dbg_malloc(size, desc, line);
	if(buf)
	{
		fsl_osal_memcpy(buf, ptr, bt->size - (PRE_SIZE + AFT_SIZE));
		dbg_free(ptr);
	}
	else
		dbg_free(ptr); //FIXME

	return buf;
}
Пример #2
0
void f_free(void * ptr)
{
    struct f_malloc_block * blk;

    /* stats */

    if (!ptr) {
        return;
    }

    blk = (struct f_malloc_block *)((uint8_t *)ptr - sizeof(struct f_malloc_block));
    if (blk->magic == F_MALLOC_MAGIC)
    {
        blk->flags = 0u;
        /* stats */
        f_malloc_stats[blk->flags & MEM_USER].free_calls++;
        f_malloc_stats[blk->flags & MEM_USER].objects_allocated--;
        f_malloc_stats[blk->flags & MEM_USER].mem_allocated -= (uint32_t)blk->size;
        /* Merge adjecent free blocks */
        //XXX: How to check if actually adjacent? -> Pointer arithmetic? (sizeof(struct) + blk->size) ?
        // Or just assume for now that they are adjacent? -> Like FreeRTOS heap4
        if ((blk->prev) && (!in_use(blk->prev)))
        {
            blk = merge_blocks(blk->prev, blk);
        }
        if ((blk->next) && (!in_use(blk->next)))
        {
            blk = merge_blocks(blk, blk->next);
        }
    } else {
        dbg_malloc("FREE ERR: %p is not a valid allocated pointer!\n", blk);
    }
}
Пример #3
0
/*
 * Read the size in sectors from the sysfs "size" file.
 * Avoid access to removable devices.
 */
static int
sysfs_get_size(struct lib_context *lc, struct dev_info *di,
	       const char *path, char *name)
{
	int ret = 0;
	char buf[22], *sysfs_file;
	const char *sysfs_size = "size";
	FILE *f;

	if (!(sysfs_file = dbg_malloc(strlen(path) + strlen(name) +
				      strlen(sysfs_size) + 3)))
		return log_alloc_err(lc, __func__);

	sprintf(sysfs_file, "%s/%s/%s", path, name, sysfs_size);
	if ((f = fopen(sysfs_file, "r"))) {
		/* Use fread+sscanf for klibc compatibility. */
		if (fread(buf, sizeof(char), sizeof buf - 1, f) &&
		    (ret = sscanf(buf, "%" PRIu64, &di->sectors)) != 1) {
			ret = 0;
			log_err(lc, "reading disk size for %s from sysfs",
				di->path);
		}

		fclose(f);
	} else
		log_err(lc, "opening %s", sysfs_file);

	dbg_free(sysfs_file);

	return ret;
}
Пример #4
0
/* Fake a SCSI serial number by reading it from a file. */
static int
get_dm_test_serial(struct lib_context *lc, struct dev_info *di, char *path)
{
	int ret = 1;
	char *serial, buffer[32];
	const char *dot_serial = ".serial";
	FILE *f;

	if (!(serial = dbg_malloc(strlen(path) + strlen(dot_serial) + 1)))
		return log_alloc_err(lc, __func__);

	sprintf(serial, "%s%s", path, dot_serial);
	if ((f = fopen(serial, "r")) &&
	    fgets(buffer, 31, f) &&
	    !(di->serial = dbg_strdup(remove_white_space(lc, buffer,
							 strlen(buffer)))))
		ret = 0;

	dbg_free(serial);
	if (f)
		fclose(f);
	else
		log_warn(lc, "missing dm serial file for %s", di->path);

	return ret;
#undef	DOT_SERIAL
}
Пример #5
0
int SaveChannelSetToOpenHandle(WaWEChannelSet_p pChannelSet, WaWEPlugin_p pPlugin, void *hFile, HWND hwndSavingDlg)
{
  char *pchReadBuffer;
  int   iBytesPerSample;
  int   iNumOfChannels;
  WaWEPosition_t  CurrPos;
  WaWESize_t      SamplesToRead;
  WaWESize_t      ReadedSamples;
  int rc;

  printf("[SaveChannelSetToOpenHandle] : Enter\n");

  pchReadBuffer = (char *) dbg_malloc(READ_BUF_SIZE);
  if (!pchReadBuffer)
  {
    printf("[SaveChannelSetToOpenHandle] : Out of memory to allocate pchReadBuffer!\n");
    return 0;
  }

  iNumOfChannels = GetNumOfChannelsFromChannelSet(pChannelSet);
  CurrPos = 0;
  iBytesPerSample = (pChannelSet->OriginalFormat.iBits + 7)/8;
  SamplesToRead = READ_BUF_SIZE / iBytesPerSample / iNumOfChannels;
  while (CurrPos<pChannelSet->Length)
  {
    ReadedSamples = 0;
    WaWEHelper_ChannelSet_ReadPackedSamples(pChannelSet,
                                            CurrPos,
                                            SamplesToRead,
                                            pchReadBuffer,
                                            &(pChannelSet->OriginalFormat),
                                            &ReadedSamples);
    if (ReadedSamples==0) break;
    CurrPos+=ReadedSamples;

    if (pChannelSet->Length>0)
    {
#ifdef DEBUG_BUILD
      printf("[SaveChannelSetToOpenHandle] : Progress: %d%%\n", CurrPos * 100 / pChannelSet->Length);
#endif

      WinSendDlgItemMsg(hwndSavingDlg, DID_SLIDER, SLM_SETSLIDERINFO,
                        MPFROM2SHORT(SMA_SLIDERARMPOSITION, SMA_INCREMENTVALUE),
                        (MPARAM) (CurrPos * 99 / pChannelSet->Length));
    }

    rc = pPlugin->TypeSpecificInfo.ExportPluginInfo.fnWrite(hFile, pchReadBuffer, ReadedSamples*iBytesPerSample*iNumOfChannels);
    if (!rc)
    {
      dbg_free(pchReadBuffer);
      printf("[SaveChannelSetToOpenHandle] : Error from fnWrite() of export plugin!\n");
      return 0;
    }
  }

  dbg_free(pchReadBuffer);

  printf("[SaveChannelSetToOpenHandle] : Done\n");
  return 1; // Success
}
Пример #6
0
void *dbg_realloc(void *buf, size_t s)
{
  struct head *p;
  realloc_cnt++;
  if(buf) /* when buf is NULL do malloc. counted twice as r and m */
    if(s)  /* when s = 0 realloc() acts like free(). counted twice: as r and f */
      if((p = find_in_heap(buf)))
	{
	  buf = realloc(buf, s);
	  if(buf)
	    {
	      replace(p, buf, s);
	      return buf;
	    }
	  else
	    fprintf(stderr, "%s:%lu: dbg_realloc: not enough memory\n",
		    dbg_file_name, dbg_line_number);
	}
      else
	dbg_check_addr("dbg_realloc", buf, CHK_FREED);
    else
      dbg_free(buf);
  else
    return dbg_malloc(s);
  return NULL;
}
Пример #7
0
WaWEPluginList_p CreatePluginList(int iPluginType)
{
  WaWEPluginList_p pResult = NULL;
  WaWEPluginList_p pLast = NULL;
  WaWEPluginList_p pNew;
  WaWEPlugin_p temp;

  if (DosRequestMutexSem(PluginListProtector_Sem, SEM_INDEFINITE_WAIT)==NO_ERROR)
  {
    temp = PluginListHead;
    while (temp)
    {
      if (temp->iPluginType == iPluginType)
      {
        pNew = (WaWEPluginList_p) dbg_malloc(sizeof(WaWEPluginList_t));
        if (pNew)
        {
          pNew->pNext = NULL;
          pNew->pPlugin = temp;
          if (!pResult)
          {
            pResult = pNew;
          } else
          {
            pLast->pNext = pNew;
          }
          pLast = pNew;
        }
      }
      temp = temp->pNext;
    }
    DosReleaseMutexSem(PluginListProtector_Sem);
  }
  return pResult;
}
Пример #8
0
/* Make up a dm path. */
char *
mkdm_path(struct lib_context *lc, const char *name)
{
	char *ret;
	const char *dir = dm_dir();

	if ((ret = dbg_malloc(strlen(dir) + strlen(name) + 2)))
		sprintf(ret, "%s/%s", dir, name);
	else
		log_alloc_err(lc, __func__);

	return ret;
}
Пример #9
0
/*!
 * \brief Create a copy of a string.
 * 
 * Allocates sufficient memory from heap for a copy of the string
 * does the copy.
 *
 * \param str Pointer to the string to copy.
 *
 * \return A pointer to the new string or NULL if allocating memory failed.
 */
char *dbg_strdup(CONST char *str, CONST char *file, int line)
{
    size_t siz;
    char *copy;

    siz = strlen(str) + 1;
    if ((copy = dbg_malloc(siz, file, line)) == NULL) {
        return NULL;
    }
    memcpy(copy, str, siz);

    return copy;
}
Пример #10
0
/* Figure out a name for the RAID device. */
static char *
name(struct lib_context *lc, struct asr *asr)
{
	size_t len;
	char *ret;

	if ((ret = dbg_malloc((len = _name(lc, asr, NULL, 0) + 1))))
		_name(lc, asr, ret, len);
	else
		log_alloc_err(lc, handler);

	return ret;
}
Пример #11
0
char *dbg_strndup(const char *str, size_t n)
{
  size_t len;
  char *buf;
  len = strlen(str);
  if(len > n) len = n;
  buf = dbg_malloc(len + 1);
  if(buf)
    {
      memcpy(buf, str, len);
      buf[len] = 0;
    }
  return buf;
}
Пример #12
0
/*------------------*/


#if defined __linux__ || defined _WIN32 /* test application */
    void print_malloc_stats(void)
    {
        dbg_malloc("\n=== FROSTED MALLOC STATS ===\n");
        dbg_malloc("--> malloc calls: %d\n", f_malloc_stats[0].malloc_calls);
        dbg_malloc("--> free   calls: %d\n", f_malloc_stats[0].free_calls);
        dbg_malloc("--> objects allocated: %d\n", f_malloc_stats[0].objects_allocated);
        dbg_malloc("--> memory  allocated: %d\n", f_malloc_stats[0].mem_allocated);
        dbg_malloc("=== FROSTED MALLOC STATS ===\n\n");
    }
Пример #13
0
/* Make up an absolute sysfs path given a relative one. */
static char *
mk_sysfs_path(struct lib_context *lc, char const *path)
{
	static char *ret = NULL, *sysfs_mp;

	if (!(sysfs_mp = find_sysfs_mp(lc)))
		LOG_ERR(lc, NULL, "finding sysfs mount point");

	if ((ret = dbg_malloc(strlen(sysfs_mp) + strlen(path) + 1)))
		sprintf(ret, "%s%s", sysfs_mp, path);
	else
		log_alloc_err(lc, __func__);

	return ret;
}
Пример #14
0
void *dbg_calloc(size_t num, size_t size, CONST char *file, int line)
{
    register void *p;

#ifdef SIZE_T_MAX
    if (num && size && SIZE_T_MAX / num < size) {
        errno = ENOMEM;
        return NULL;
    }
#endif
    size *= num;
    p = dbg_malloc(size, file, line);
    if (p)
        memset(p, 0, size);
    return (p);
}
Пример #15
0
 void print_malloc_entries(void)
 {
     struct f_malloc_block *blk = malloc_entry_kernel;
     uint32_t i = 0;
 
     /* See if we can find a free block that fits */
     while (blk) /* last entry will break the loop */
     {
         dbg_malloc(">>> Entry #%d: \n", i);
         dbg_malloc("    Address (blk) %p \n", blk);
         dbg_malloc("    Address (usr) %p \n", ((uint8_t*)blk) + sizeof(struct f_malloc_block));
         dbg_malloc("    Prev: %p \n", blk->prev);
         dbg_malloc("    Next: %p \n", blk->next);
         dbg_malloc("    Size (usr) %d \n", blk->size);
         dbg_malloc("    Flags: %08x \n", blk->flags);
         dbg_malloc("    Magic: 0x%08x \n", blk->magic);
         i++;
         blk = blk->next;
     }
 }
Пример #16
0
/* Read the whole metadata chunk at once */
static uint8_t *
read_metadata_chunk(struct lib_context *lc, struct dev_info *di, uint64_t start)
{
	uint8_t *ret;
	size_t size = (di->sectors - start) * ASR_DISK_BLOCK_SIZE;

	if (!(ret = dbg_malloc(size)))
		LOG_ERR(lc, ret, "%s: unable to allocate memory for %s",
			handler, di->path);

	if (!read_file(lc, handler, di->path, ret, size,
		       start * ASR_DISK_BLOCK_SIZE)) {
		dbg_free(ret);
		LOG_ERR(lc, NULL, "%s: unable to read metadata on %s",
			handler, di->path);
	}

	return ret;
}
Пример #17
0
/* Ask sysfs, if a device is removable. */
int
removable_device(struct lib_context *lc, char *dev_path)
{
	int ret = 0;
	char buf[2], *name, *sysfs_path, *sysfs_file;
	const char *sysfs_removable = "removable";
	FILE *f;

	if (!(sysfs_path = mk_sysfs_path(lc, BLOCK)))
		return 0;

	name = get_basename(lc, dev_path);
	if (!(sysfs_file = dbg_malloc(strlen(sysfs_path) + strlen(name) +
				      strlen(sysfs_removable) + 3))) {
		log_alloc_err(lc, __func__);
		goto out;
	}

	sprintf(sysfs_file, "%s/%s/%s", sysfs_path, name, sysfs_removable);
	if ((f = fopen(sysfs_file, "r"))) {
		/* Using fread for klibc compatibility. */
		if (fread(buf, sizeof(char), sizeof(buf) - 1, f) && *buf == '1') {
			log_notice(lc, "skipping removable device %s",
				   dev_path);
			ret = 1;
		}

		fclose(f);
	}

	dbg_free(sysfs_file);

out:
	dbg_free(sysfs_path);

	return ret;
}
Пример #18
0
static int
get_size(struct lib_context *lc, const char *path, char *name, int sysfs)
{
	int fd, ret = 0;
	char *dev_path;
	struct dev_info *di = NULL;

	if (!(dev_path = dbg_malloc(strlen(_PATH_DEV) + strlen(name) + 1)))
		return log_alloc_err(lc, __func__);

	sprintf(dev_path, "%s%s", _PATH_DEV, name);
	if (!interested(lc, dev_path)) {
		ret = 0;
		goto out;
	}

	if (removable_device(lc, dev_path) ||
	    !(di = alloc_dev_info(lc, dev_path)) ||
	    (sysfs && !sysfs_get_size(lc, di, path, name)) ||
	    (fd = open(dev_path, O_RDONLY)) == -1)
		goto out;

	if (di_ioctl(lc, fd, di)) {
		list_add(&di->list, LC_DI(lc));
		ret = 1;
	}

	close(fd);

out:
	dbg_free(dev_path);

	if (!ret && di)
		free_dev_info(lc, di);

	return ret;
}
Пример #19
0
    int main(int argc, char ** argv)
    {
        void * test10 = f_malloc(10);
        void * test200 = f_malloc(200);
        void * test100 = NULL;
        dbg_malloc("test10: %p\n", test10);
        dbg_malloc("test200: %p\n", test200);
        f_free(test10);
        print_malloc_stats();
        print_malloc_entries();
    
        dbg_malloc("\nTrying to re-use freed memory + allocate more\n");
        test10 = f_malloc(10); // this should re-use exisiting entry
        test100 = f_malloc(100); // this should alloc more memory through sbrk
        print_malloc_stats();
        print_malloc_entries();
    
        dbg_malloc("\nFreeing all of the memory\n");
        f_free(test10);
        f_free(test200);
        f_free(test100);
        print_malloc_stats();
        print_malloc_entries();

        dbg_malloc("Trying to re-use freed memory\n");
        test100 = f_malloc(100); // this should re-use memory in the freed pool
        print_malloc_stats();
        print_malloc_entries();

        dbg_malloc("Allocating more memory\n");
        test10 = f_malloc(10);
        test200 = f_malloc(200);
        print_malloc_stats();
        print_malloc_entries();
    
        return 0;
    }
Пример #20
0
int SetEventCallback(int iEventCode, pfn_WaWEP_EventCallback pEventCallback)
{
  int iResult = 1;
  WaWEEventCallbackList_p pTempEvent, pPrev;

  if (!pEventCallback) return 0;

  if (DosRequestMutexSem(CallbackListProtector_Sem, SEM_INDEFINITE_WAIT)==NO_ERROR)
  {
    pPrev = NULL;
    pTempEvent = CallbackListHead;
    while ((pTempEvent) && (pTempEvent->pEventCallback!=pEventCallback))
    {
      pPrev = pTempEvent;
      pTempEvent = pTempEvent->pNext;
    }

    if (iEventCode == WAWE_EVENT_NONE)
    {
      // The callback has to be removed from the list!
      if (pTempEvent)
      {
        // Fine, found in list, so remove it from there!
        if (pPrev)
          pPrev->pNext = pTempEvent->pNext;
        else
          CallbackListHead = pTempEvent->pNext;

        dbg_free(pTempEvent);
      } else
      {
#ifdef DEBUG_BUILD
        printf("[SetEventCallback] : Could not remove event callback (0x%p), not found in list!\n", pEventCallback);
#endif
        iResult = 0;
      }
    } else
    {
      // The callback has to be changed or has to be registered!
      if (pTempEvent)
      {
        // This callback is already in the list, so
        // only change its subscribed event type!
        pTempEvent->iEventCode = iEventCode;
      } else
      {
        pTempEvent = (WaWEEventCallbackList_p) dbg_malloc(sizeof(WaWEEventCallbackList_t));
        if (!pTempEvent)
        {
#ifdef DEBUG_BUILD
          printf("[SetEventCallback] : Out of memory, event callback (0x%p) could not be registered!\n", pEventCallback);
#endif
          iResult = 0;
        } else
        {
          pTempEvent->iEventCode = iEventCode;
          pTempEvent->pEventCallback = pEventCallback;
          pTempEvent->pNext = CallbackListHead;
          CallbackListHead = pTempEvent;
        }
      }
    }

    DosReleaseMutexSem(CallbackListProtector_Sem);
  } else
    iResult = 0;

  return iResult;
}
Пример #21
0
WaWEChannelSet_p CreateChannelSetFromOpenHandle(char *pchFileName,          // The filename
                                                WaWEPlugin_p pPlugin,       // The plugin which opened it
                                                WaWEImP_Open_Handle_p hFile,// The handle returned by the plugin
                                                int bRedrawNow)             // If True, then a screen redraw will be called
{
  WaWEChannelSet_p result;
  WaWEFormat_t OriginalFormat;
  WaWEChannel_p pChannel;
  WaWESize_t ChannelLength;
  int iChannelNum;
  int rc;

  OriginalFormat.iFrequency = hFile->iFrequency;
  OriginalFormat.iBits      = hFile->iBits;
  OriginalFormat.iIsSigned  = hFile->iIsSigned;

  rc = WaWEHelper_Global_CreateChannelSet(&result,
                                          &OriginalFormat,
                                          hFile->iChannels,
                                          hFile->pchProposedChannelSetName,
                                          pchFileName);
  if (rc!=WAWEHELPER_NOERROR)
  {
    return result; // If all goes well, it's already NULL
  }

  // Ok, an empty ChannelSet has been created, now
  // fill it with data!
  if (DosRequestMutexSem(ChannelSetListProtector_Sem, SEM_INDEFINITE_WAIT)==NO_ERROR)
  {
    // Save plugin and file handle, used to create the channel, to be able
    // to close file handle later!
    result->pOriginalPlugin = pPlugin;
    result->hOriginalFile   = hFile;

    // Initialize read-cache
    result->ChannelSetCache.bUseCache = WaWEConfiguration.bUseCache;
    result->ChannelSetCache.uiCachePageSize = WaWEConfiguration.uiCachePageSize;
    result->ChannelSetCache.uiNumOfCachePages = WaWEConfiguration.uiNumOfCachePages;
    result->ChannelSetCache.FileSize = 0; // Will be filled later!
    if (result->ChannelSetCache.bUseCache)
    {
      // Initialize cache pages
      result->ChannelSetCache.CachePages =
        (WaWEChannelSetCachePage_p) dbg_malloc(sizeof(WaWEChannelSetCachePage_t) * result->ChannelSetCache.uiNumOfCachePages);

      if (!result->ChannelSetCache.CachePages)
      {
#ifdef DEBUG_BUILD
        printf("[CreateChannelSetFromOpenHandle] : Out of memory, cache disabled for this channel-set!\n");
#endif
        result->ChannelSetCache.bUseCache = FALSE;
      } else
      {
        unsigned int uiTemp;
        WaWEChannelSetCachePage_p pCachePages;

        pCachePages = result->ChannelSetCache.CachePages;
        // Initialize cache-pages to be empty
        for (uiTemp = 0; uiTemp<result->ChannelSetCache.uiNumOfCachePages; uiTemp++)
        {
          pCachePages[uiTemp].PageNumber = -1;
          pCachePages[uiTemp].uiNumOfBytesOnPage = 0;
          pCachePages[uiTemp].puchCachedData = NULL;
        }
      }
    } else
    {
      // No cache pages to use, so set cache pages to NULL
      result->ChannelSetCache.CachePages = NULL;
    }

    // Move (!!) the channel-set format pointer to channel-set level from
    // file handle level. This makes it sure that bad import plugins
    // will not free it at close-time, and we won't try to free it again
    // when destroying the channel-set structure.
    WaWEHelper_ChannelSet_StartChSetFormatUsage(result);
    result->ChannelSetFormat = hFile->ChannelSetFormat;
    hFile->ChannelSetFormat = NULL;
    WaWEHelper_ChannelSet_StopChSetFormatUsage(result, TRUE);
    // Remove "modified" flag from channel-set!
    result->bModified = FALSE;

    // Calculate number of samples in one channel!
    ChannelLength = pPlugin->TypeSpecificInfo.ImportPluginInfo.fnSeek(hFile, 0, WAWE_SEEK_END);
    ChannelLength++; // Last position + 1 will mean the number of bytes in file.
    result->ChannelSetCache.FileSize = ChannelLength;
#ifdef DEBUG_BUILD
    printf("FileSize = %d\n", ChannelLength);
#endif
    pPlugin->TypeSpecificInfo.ImportPluginInfo.fnSeek(hFile, 0, WAWE_SEEK_SET);
    ChannelLength /= (result->OriginalFormat.iBits/8);
    ChannelLength /= hFile->iChannels;
    result->Length = ChannelLength;
#ifdef DEBUG_BUILD
    printf("ChannelLength = %d\n", ChannelLength);
#endif

    // Setup the list of channels inside the channel-set!
    pChannel = result->pChannelListHead;
    iChannelNum = 0;
    while ((pChannel) && (iChannelNum<hFile->iChannels))
    {
      char *pchProposedName;

      if ((hFile->apchProposedChannelName) &&
          (hFile->apchProposedChannelName[iChannelNum]))
        pchProposedName = hFile->apchProposedChannelName[iChannelNum];
      else
        pchProposedName = NULL;

      // Set initial values of channel
      memcpy(&(pChannel->VirtualFormat), &(result->OriginalFormat), sizeof(pChannel->VirtualFormat));

      if (pchProposedName)
        strncpy(pChannel->achName, pchProposedName, sizeof(pChannel->achName));

      pChannel->Length = ChannelLength;
      pChannel->FirstSamplePos = 0;
      pChannel->LastSamplePos = result->iWidth-1;

      if (DosRequestMutexSem(pChannel->hmtxUseChunkList, SEM_INDEFINITE_WAIT)==NO_ERROR)
      {

#ifdef DEBUG_BUILD
        if (pChannel->pChunkListHead)
        {
          printf("Warning: Empty channel already has a chunk!!!\n"); fflush(stdout);
        }
#endif
        pChannel->pChunkListHead = (WaWEChunk_p) dbg_malloc(sizeof(WaWEChunk_t));
        if (pChannel->pChunkListHead)
        {
          // At the beginning, the channel will contain one big chunk.
          pChannel->pChunkListHead->iChunkType = WAWE_CHUNKTYPE_ORIGINAL;
          pChannel->pChunkListHead->VirtPosStart = 0;
          pChannel->pChunkListHead->VirtPosEnd = ChannelLength-1;
#ifdef DEBUG_BUILD
          printf("Chunk is from %d to %d\n", 0, ChannelLength-1);
#endif
          pChannel->pChunkListHead->pPlugin = pPlugin;
          pChannel->pChunkListHead->hFile = hFile;
          pChannel->pChunkListHead->iChannelNum = iChannelNum;
          pChannel->pChunkListHead->RealPosStart = 0;
          pChannel->pChunkListHead->pSampleBuffer = NULL;
          pChannel->pChunkListHead->pPrev = NULL;
          pChannel->pChunkListHead->pNext = NULL;
        }

        DosReleaseMutexSem(pChannel->hmtxUseChunkList);
      }
#ifndef OPTIMIZE_REDRAW_AT_OPEN
      // Note that the channel has been changed, so
      // redraw the channel.
      pChannel->bRedrawNeeded = 1;
#endif

      iChannelNum++;
      pChannel = pChannel->pNext;
    }

    // Notify that the channel-set has been changed
    // This will update channel-set length
    result->bRedrawNeeded = 1;

    // Return the result
    DosReleaseMutexSem(ChannelSetListProtector_Sem);

    if (bRedrawNow)
    {
      // Redraw everything that's changed!
      RedrawChangedChannelsAndChannelSets();
    }
  }

  return result;
}
Пример #22
0
void * f_malloc(int flags, size_t size)
{
    struct f_malloc_block * blk = NULL, *last = NULL;
    int user = 0;

    if (flags & MEM_USER) {
        user = 1;
    }

    while((size % 4) != 0) {
        size++;
    } 

    /* update stats */
    f_malloc_stats[flags & MEM_USER].malloc_calls++;

    /* Travel the linked list for first fit */
    blk = f_find_best_fit(user, size, &last);
    if (blk)
    {
        dbg_malloc("Found best fit!\n");
        /* first fit found, now split it if it's much bigger than needed */
        if (size + (2*sizeof(struct f_malloc_block)) < blk->size)
        {
            dbg_malloc("Splitting blocks, since requested size [%d] << best fit block size [%d]!\n", size, blk->size);
            split_block(blk, size);
        }
    } else {
        /* No first fit found: ask for new memory */
        blk = (struct f_malloc_block *)f_sbrk(user, size + sizeof(struct f_malloc_block));  // can OS give us more memory?
        if ((long)blk == -1)
            return NULL;

        /* first call -> set entrypoint */
        if ((user) && (malloc_entry_user == NULL))
        {
            malloc_entry_user = blk;
            blk->prev = NULL;
        }
        if ((!user) && (malloc_entry_kernel == NULL))
        {
            malloc_entry_kernel = blk;
            blk->prev = NULL;
        }


        blk->magic = F_MALLOC_MAGIC;
        blk->size = size;
        blk->next = NULL; /* Newly allocated memory is always last in the linked list */
        /* Link this entry to the previous last entry */
        if (last)
        {
            // assert(last->next == NULL);
            last->next = blk;
            blk->prev = last;
        }
    }

    /* destination found, fill in  meta-data */
    blk->flags = F_IN_USE | flags;

    /* update stats */
    f_malloc_stats[flags & MEM_USER].objects_allocated++;
    f_malloc_stats[flags & MEM_USER].mem_allocated += (uint32_t)blk->size;

    return (void *)(((uint8_t *)blk) + sizeof(struct f_malloc_block)); // pointer to newly allocated mem
}
Пример #23
0
void *
dbg_realloc(void *buffer, unsigned long size, char *file, char *function,
		   int line)
{
	int i = 0;
	void *buf, *buf2;
	long check;

	if (buffer == NULL) {
		printf("realloc %p %s, %3d %s\n", buffer, file, line, function);
		i = number_alloc_list++;

		assert(number_alloc_list < MAX_ALLOC_LIST);

		alloc_list[i].ptr = NULL;
		alloc_list[i].size = 0;
		alloc_list[i].file = file;
		alloc_list[i].func = function;
		alloc_list[i].line = line;
		alloc_list[i].type = 3;
		return dbg_malloc(size, file, function, line);
	}

	buf = buffer;

	while (i < number_alloc_list) {
		if (alloc_list[i].ptr == buf) {
			buf = alloc_list[i].ptr;
			break;
		}
		i++;
	}

	/* not found */
	if (i == number_alloc_list) {
		printf("realloc ERROR no matching zalloc %p \n", buffer);
		number_alloc_list++;

		assert(number_alloc_list < MAX_ALLOC_LIST);

		alloc_list[i].ptr = buf;
		alloc_list[i].size = 0;
		alloc_list[i].file = file;
		alloc_list[i].func = function;
		alloc_list[i].line = line;
		alloc_list[i].type = 9;
		debug |= 512;	/* Memory Error detect */
		return NULL;
	}

	buf2 = ((char *) buf) + alloc_list[i].size;

	if (*(long *) (buf2) != alloc_list[i].csum) {
		alloc_list[i].type = 1;
		debug |= 512;	/* Memory Error detect */
	}
	buf = realloc(buffer, size + sizeof (long));

	check = 0xa5a5 + size;
	*(long *) ((char *) buf + size) = check;
	alloc_list[i].csum = check;

	if (debug & 1)
		printf("realloc [%3d:%3d] %p, %4ld %s %d %s -> %p %4ld %s %d %s\n",
		       i, number_alloc_list, alloc_list[i].ptr,
		       alloc_list[i].size, alloc_list[i].file, alloc_list[i].line, alloc_list[i].func,
		       buf, size, file, line, function);

	alloc_list[i].ptr = buf;
	alloc_list[i].size = size;
	alloc_list[i].file = file;
	alloc_list[i].line = line;
	alloc_list[i].func = function;

	return buf;
}
Пример #24
0
void * operator new(unsigned int size, char* file, int line)
{
	return dbg_malloc(size, file, line);
}
Пример #25
0
int UpdateReOpenedChannelSet(WaWEChannelSet_p pChannelSet, int bRedrawNow)
{
  WaWEPlugin_p pPlugin;
  WaWEImP_Open_Handle_p  hFile;
  WaWEChannel_p pChannel;
  WaWESize_t ChannelLength;
  int iChannelNum;
  int bOldModifiedFlag;
  int rc;

  pPlugin = pChannelSet->pOriginalPlugin;
  hFile = pChannelSet->hOriginalFile;

  pChannelSet->OriginalFormat.iFrequency = hFile->iFrequency;
  pChannelSet->OriginalFormat.iBits      = hFile->iBits;
  pChannelSet->OriginalFormat.iIsSigned  = hFile->iIsSigned;

  bOldModifiedFlag = pChannelSet->bModified;
  WaWEHelper_ChannelSet_StartChSetFormatUsage(pChannelSet);
  WaWEHelper_ChannelSetFormat_DeleteAllKeys(&(pChannelSet->ChannelSetFormat));
  pChannelSet->ChannelSetFormat = hFile->ChannelSetFormat;
  hFile->ChannelSetFormat = NULL;
  WaWEHelper_ChannelSet_StopChSetFormatUsage(pChannelSet, TRUE);
  pChannelSet->bModified = bOldModifiedFlag;

  // Make the channel-set cache empty, if turned on!
  if (pChannelSet->ChannelSetCache.bUseCache)
  {
    WaWEChannelSetCachePage_p pCachePages;
    unsigned int uiTemp;

    pCachePages = pChannelSet->ChannelSetCache.CachePages;

    for (uiTemp=0; uiTemp<pChannelSet->ChannelSetCache.uiNumOfCachePages; uiTemp++)
    {
      if (pCachePages->puchCachedData)
      {
        dbg_free(pCachePages->puchCachedData);
        pCachePages->puchCachedData = NULL;
      }
      pCachePages->uiNumOfBytesOnPage = 0;
      pCachePages->PageNumber = -1;
    }
  }

  // Calculate number of samples in one channel!
  ChannelLength = pPlugin->TypeSpecificInfo.ImportPluginInfo.fnSeek(hFile, 0, WAWE_SEEK_END);
  ChannelLength++; // Last position + 1 will mean the number of bytes in file.
  pChannelSet->ChannelSetCache.FileSize = ChannelLength; // Also update cache!
#ifdef DEBUG_BUILD
  printf("[UpdateReOpenedChannelSet] : Enter\n");
  printf("FileSize = %d\n", ChannelLength);
#endif
  pPlugin->TypeSpecificInfo.ImportPluginInfo.fnSeek(hFile, 0, WAWE_SEEK_SET);
  ChannelLength /= (pChannelSet->OriginalFormat.iBits/8);
  ChannelLength /= hFile->iChannels;
  pChannelSet->Length = ChannelLength;

#ifdef DEBUG_BUILD
  printf("ChannelLength = %d\n", ChannelLength);
  printf("Number of channels = %d\n", hFile->iChannels);
#endif

  // Setup the list of channels inside the channel-set!
  pChannel = pChannelSet->pChannelListHead;
  iChannelNum = 0;
  while ((pChannel) && (iChannelNum<hFile->iChannels))
  {
#ifdef DEBUG_BUILD
    printf("Updating channel %p (Channel num %d)\n", pChannel, hFile->iChannels); fflush(stdout);
#endif

    memcpy(&(pChannel->VirtualFormat), &(pChannelSet->OriginalFormat), sizeof(pChannel->VirtualFormat));

    pChannel->Length = ChannelLength;

    if (DosRequestMutexSem(pChannel->hmtxUseChunkList, SEM_INDEFINITE_WAIT)==NO_ERROR)
    {
      WaWEChunk_p pToDelete;

      while (pChannel->pChunkListHead)
      {
        pToDelete = pChannel->pChunkListHead;
        pChannel->pChunkListHead = pChannel->pChunkListHead->pNext;

        if (pToDelete->iChunkType == WAWE_CHUNKTYPE_MODIFIED)
        {
          // Delete the modified chunk
          if (pToDelete->pSampleBuffer)
          {
            dbg_free(pToDelete->pSampleBuffer); pToDelete->pSampleBuffer = NULL;
          }
        } else
        {
          // Delete the original chunk
          // Nothing special to do here, anyway, we'll recreate one original chunk.
        }
        dbg_free(pToDelete);
      }

      pChannel->pChunkListHead = (WaWEChunk_p) dbg_malloc(sizeof(WaWEChunk_t));
      if (pChannel->pChunkListHead)
      {
        // At the beginning, the channel will contain one big chunk.
        pChannel->pChunkListHead->iChunkType = WAWE_CHUNKTYPE_ORIGINAL;
        pChannel->pChunkListHead->VirtPosStart = 0;
        pChannel->pChunkListHead->VirtPosEnd = ChannelLength-1;
#ifdef DEBUG_BUILD
        printf("Chunk is from %d to %d\n", 0, ChannelLength-1);
#endif
        pChannel->pChunkListHead->pPlugin = pPlugin;
        pChannel->pChunkListHead->hFile = hFile;
        pChannel->pChunkListHead->iChannelNum = iChannelNum;
        pChannel->pChunkListHead->RealPosStart = 0;
        pChannel->pChunkListHead->pSampleBuffer = NULL;
        pChannel->pChunkListHead->pPrev = NULL;
        pChannel->pChunkListHead->pNext = NULL;
      }

      DosReleaseMutexSem(pChannel->hmtxUseChunkList);
    }

#ifndef OPTIMIZE_REDRAW_AT_OPEN
    // Note that the channel has been changed, so
    // redraw the channel.
    pChannel->bRedrawNeeded = 1;
#endif

    iChannelNum++;
    pChannel = pChannel->pNext;
  }

  // Ok, out of loop.
  // Now, delete every extra channel which remained in channel-set!
  while (pChannel)
  {
    WaWEChannel_p pToDelete;
    pToDelete = pChannel;
    pChannel = pChannel->pNext;
#ifdef DEBUG_BUILD
    printf("Deleting old channel %p\n", pToDelete);
#endif
    WaWEHelper_ChannelSet_DeleteChannel(pToDelete);
  }
  // Now, create every extra channel which appeared in file!
  while (iChannelNum<hFile->iChannels)
  {
#ifdef DEBUG_BUILD
    printf("Creating new channel num %d\n", hFile->iChannels);
#endif

    rc = WaWEHelper_ChannelSet_CreateChannel(pChannelSet,
                                             WAWECHANNEL_BOTTOM,
                                             NULL,
                                             &pChannel);
    if (rc==WAWEHELPER_NOERROR)
    {
      // Fill new channel with info!
      char *pchProposedName;

      if ((hFile->apchProposedChannelName) &&
          (hFile->apchProposedChannelName[iChannelNum]))
        pchProposedName = hFile->apchProposedChannelName[iChannelNum];
      else
        pchProposedName = NULL;

      // Set initial values of channel
      memcpy(&(pChannel->VirtualFormat), &(pChannelSet->OriginalFormat), sizeof(pChannel->VirtualFormat));

      if (pchProposedName)
        strncpy(pChannel->achName, pchProposedName, sizeof(pChannel->achName));

      pChannel->Length = ChannelLength;
      if (pChannelSet->pChannelListHead)
      {
        // Copy settings from other channels
        pChannel->FirstSamplePos = pChannelSet->pChannelListHead->FirstSamplePos;
        pChannel->LastSamplePos = pChannelSet->pChannelListHead->LastSamplePos;
      } else
      {
        pChannel->FirstSamplePos = 0;
        pChannel->LastSamplePos = pChannelSet->iWidth-1;
      }

      if (DosRequestMutexSem(pChannel->hmtxUseChunkList, SEM_INDEFINITE_WAIT)==NO_ERROR)
      {

        WaWEChunk_p pToDelete;
  
        while (pChannel->pChunkListHead)
        {
          pToDelete = pChannel->pChunkListHead;
          pChannel->pChunkListHead = pChannel->pChunkListHead->pNext;
  
          if (pToDelete->pSampleBuffer)
            dbg_free(pToDelete->pSampleBuffer);
          dbg_free(pToDelete);
        }

        pChannel->pChunkListHead = (WaWEChunk_p) dbg_malloc(sizeof(WaWEChunk_t));
        if (pChannel->pChunkListHead)
        {
          // At the beginning, the channel will contain one big chunk.
          pChannel->pChunkListHead->iChunkType = WAWE_CHUNKTYPE_ORIGINAL;
          pChannel->pChunkListHead->VirtPosStart = 0;
          pChannel->pChunkListHead->VirtPosEnd = ChannelLength-1;
#ifdef DEBUG_BUILD
          printf("Chunk is from %d to %d\n", 0, ChannelLength-1);
#endif
          pChannel->pChunkListHead->pPlugin = pPlugin;
          pChannel->pChunkListHead->hFile = hFile;
          pChannel->pChunkListHead->iChannelNum = iChannelNum;
          pChannel->pChunkListHead->RealPosStart = 0;
          pChannel->pChunkListHead->pSampleBuffer = NULL;
          pChannel->pChunkListHead->pPrev = NULL;
          pChannel->pChunkListHead->pNext = NULL;
        }

        DosReleaseMutexSem(pChannel->hmtxUseChunkList);
      }
#ifndef OPTIMIZE_REDRAW_AT_OPEN
      // Note that the channel has been changed, so
      // redraw the channel.
      pChannel->bRedrawNeeded = 1;
#endif
    }
    iChannelNum++;
  }

  // Notify that the channel-set has been changed
  // This will update channel-set length
  pChannelSet->bRedrawNeeded = 1;

  if (bRedrawNow)
  {
    // Redraw everything that's changed!
    RedrawChangedChannelsAndChannelSets();
  }
#ifdef DEBUG_BUILD
  printf("[UpdateReOpenedChannelSet] : Leave\n");
#endif
  return 1;
}
Пример #26
0
WaWEPlugin_p CreatePluginListElement(HWND hwndOwner, char *filename)
{
  HMODULE DLLHandle;
  APIRET rc;
  pfn_WaWEP_GetPluginInfo pDLL_GetPluginInfo;
  WaWEPlugin_p result = NULL;
  WaWEPluginHelpers_t PluginHelpers;

  // Open the DLL
  DLLHandle = NULLHANDLE;
  rc = DosLoadModule(NULL, 0, filename, &DLLHandle);
  if (rc!=NO_ERROR) return NULL;

  // Get the entry point
  rc = DosQueryProcAddr(DLLHandle, 0, "WaWE_GetPluginInfo", (PFN *) &pDLL_GetPluginInfo);
  if (rc==NO_ERROR)
  {
    result = (WaWEPlugin_p) dbg_malloc(sizeof(WaWEPlugin_t));
    if (!result)
    {
      DosFreeModule(DLLHandle);
      return NULL;
    }

    // Fill the info block about this dll:
    result->hmodDLL = DLLHandle;
    result->pPrev = NULL;
    result->pNext = NULL;

    // Then the info block (query from DLL)
    if (!((*pDLL_GetPluginInfo)(sizeof(WaWEPlugin_t), result, WAWE_APPVERSIONMAJOR, WAWE_APPVERSIONMINOR)))
    {
#ifdef DEBUG_BUILD
//      printf("[CreatePluginListElement] : GetPluginInfo() reported error!\n");
#endif
      dbg_free(result);
      DosFreeModule(DLLHandle);
      return NULL;
    }

    // Now initialize the plugin (if it requires it!)
    if (result->fnInitializePlugin)
    {
      FillPluginHelpersStructure(&PluginHelpers);
      if (!(result->fnInitializePlugin(hwndOwner, &PluginHelpers)))
      {
#ifdef DEBUG_BUILD
//        printf("[CreatePluginListElement] : fnInitializePlugin() reported error!\n");
#endif
        dbg_free(result);
        DosFreeModule(DLLHandle);
        return NULL;
      }
    }

#ifdef DEBUG_BUILD
//    printf("[CreatePluginListElement] : Plugin [%s] loaded, importance is %d\n",
//          filename, result->iPluginImportance);
#endif

    // Make sure the plugin does not overwrite these fields:
    result->hmodDLL = DLLHandle;
    result->pPrev = NULL;
    result->pNext = NULL;
  }
  // That was all for today.
  return result;
}