Example #1
0
/*
 * Duplicate a string, counting memory usage by type.
 * Effects: The string is duplicated, and the return value must
 * eventually be passed to zfree with the same type.  The function will
 * succeed or abort.
 */
char *
zstrdup (int type, const char *str)
{
  void *dup;

  dup = strdup (str);
  if (dup == NULL)
    zerror ("strdup", type, strlen (str));
  alloc_inc (type);
  return dup;
}
Example #2
0
/* Memory allocation. */
void *
zmalloc (int type, size_t size)
{
  void *memory;

  memory = malloc (size);
#ifdef BRCM_RIP_DEBUG
  if (memory == NULL)
    zerror ("malloc", type, size);
  alloc_inc (type);
#endif
  return memory;
}
Example #3
0
/* String duplication. */
char *
zstrdup (int type, char *str)
{
  void *dup;

  dup = strdup (str);
#ifdef BRCM_RIP_DEBUG
  if (dup == NULL)
    zerror ("strdup", type, strlen (str));
  alloc_inc (type);
#endif
  return dup;
}
Example #4
0
/*
 * Allocate memory as in zmalloc, and also clear the memory.
 */
void *
zcalloc (int type, size_t size)
{
  void *memory;

  memory = calloc (1, size);

  if (memory == NULL)
    zerror ("calloc", type, size);

  alloc_inc (type);

  return memory;
}
/**
 *  lädt die Mapdaten aus einer Datei.
 *
 *  @param[in] file Dateihandle der Datei
 *  @param[in] only_header Soll nur der Header gelesen werden?
 *
 *	@return liefert Null bei Erfolg, ungleich Null bei Fehler
 *
 *  @author FloSoft
 */
int glArchivItem_Map::load(FILE *file, bool only_header)
{
	if(loadHelper(file, only_header) != 0)
		return 1;

	alloc_inc(2);

	header = dynamic_cast<const libsiedler2::ArchivItem_Map_Header*>(get(0));
	assert(header);

	if(only_header)
		return 0;

	// Noch nicht am Ende der Datei?
	unsigned i = 0,j = 0;
	//if(!feof(file))
	//{
	//	// Gucken, wieviel noch danach kommt
	//	i = ftell(file);
	//	fseek(file, 0L, SEEK_END);
	//	j = ftell(file);
	//	fseek(file, i, SEEK_SET);
	//}

	if((unsigned int)(j-i) > (unsigned int)(header->getWidth() * header->getHeight() * 2))
	{
		// Wenn noch Platz ist, restliches Zeug noch auslesen
		libsiedler2::ArchivItem_Raw *reservations = dynamic_cast<libsiedler2::ArchivItem_Raw *>(glAllocator(libsiedler2::BOBTYPE_RAW, 0, NULL));
		if(reservations->load(file, header->getWidth() * header->getHeight()) != 0)
			return 2;
		set(MAP_RESERVATIONS, reservations);

		libsiedler2::ArchivItem_Raw *owner = dynamic_cast<libsiedler2::ArchivItem_Raw *>(glAllocator(libsiedler2::BOBTYPE_RAW, 0, NULL));
		if(owner->load(file, header->getWidth() * header->getHeight()) != 0)
			return 3;
		set(MAP_OWNER, owner);
	}
	else
	{
		libsiedler2::ArchivItem_Raw *item = dynamic_cast<libsiedler2::ArchivItem_Raw *>(glAllocator(libsiedler2::BOBTYPE_RAW, 0, NULL));
		item->alloc(header->getWidth() * header->getHeight());

		set(MAP_RESERVATIONS, item);
		setC(MAP_OWNER, item);
	}

	return 0;
}
Example #6
0
/* 
 * Given a pointer returned by zmalloc or zzcalloc, free it and
 * return a pointer to a new size, basically acting like realloc().
 * Requires: ptr was returned by zmalloc, zzcalloc, or zrealloc with the
 * same type.
 * Effects: Returns a pointer to the new memory, or aborts.
 */
void *
zrealloc (int type, void *ptr, size_t size)
{
  void *memory;

  if (ptr == NULL)              /* is really alloc */
      return zzcalloc(type, size);

  memory = realloc (ptr, size);
  if (memory == NULL)
    zerror ("realloc", type, size);
  if (ptr == NULL)
    alloc_inc (type);

  return memory;
}
Example #7
0
/* String duplication. */
char *
zebra_strdup (int type, const char *str)
{
    void *dup;

    dup = strdup (str);
    if(debug_mem)
    {
        FILE *debug_fp = fopen(logfilename, "a");
        if(debug_fp)
            fprintf(debug_fp, "strdup at        : %p ,type: %s\n", str, typestr[type - 1]);
        fclose(debug_fp);
    }
    if (dup == NULL)
        zebra_error ("strdup", type, strlen (str));
    alloc_inc (type);
    return dup;
}
Example #8
0
/* Memory allocation with num * size with cleared. */
void *
zebra_calloc (int type, size_t size)
{
    void *memory;

    memory = calloc (1, size);
    if(debug_mem)
    {
        FILE *debug_fp = fopen(logfilename, "a");
        if(debug_fp)
            fprintf(debug_fp, "calloc memory at : %p ,type: %s\n", memory, typestr[type - 1]);
        fclose(debug_fp);
    }
    if (memory == NULL)
        zebra_error ("calloc", type, size);

    alloc_inc (type);

    return memory;
}