Exemplo n.º 1
0
/* ************************************************************************ */
BlockHeap *
BlockHeapCreate(size_t elemsize, int elemsperblock)
{
    BlockHeap *bh;
    assert(elemsize > 0 && elemsperblock > 0);

    /* Catch idiotic requests up front */
    if ((elemsize <= 0) || (elemsperblock <= 0))
      {
        outofmemory();          /* die.. out of memory */
      }

    /* Allocate our new BlockHeap */
    bh = (BlockHeap *) calloc(1, sizeof(BlockHeap));
    if (bh == NULL)
      {
        outofmemory();          /* die.. out of memory */
      }

#ifdef MEMDEBUG
    elemsize += sizeof(MemoryEntry);
#endif
    if((elemsize % sizeof(void *)) != 0)
      {
	/* Pad to even pointer boundary */
	elemsize += sizeof(void *);
	elemsize &= ~(sizeof(void *) - 1);
      }

    bh->elemSize = elemsize;
    bh->elemsPerBlock = elemsperblock;
    bh->blocksAllocated = 0;
    bh->freeElems = 0;
    bh->base = NULL;

    /* Be sure our malloc was successful */
    if (newblock(bh))
      {
        if(bh != NULL)
          free(bh);
        outofmemory();          /* die.. out of memory */
      }

    if (bh == NULL)
      {
        outofmemory();          /* die.. out of memory */
      }

    return(bh);
}
Exemplo n.º 2
0
void *MyRealloc_impl(MemTracer *mt, void *obj, size_t mlen)
{
    MemTag *tag;

    if (!mt->initialized)
    {
        mt->next = mtrace_list;
        mtrace_list = mt;
        mt->initialized = 1;
    }

    if (obj)
    {
        tag = (MemTag *)obj - 1;
        tag->tracer->objects--;
        tag->tracer->allocated -= tag->length;
        obj = tag; /* subtle */
    }

    tag = realloc(obj, mlen + sizeof(MemTag));

    if (!tag)
        outofmemory();

    tag->tracer = mt;
    tag->length = mlen;
    mt->objects++;
    mt->allocated += mlen;

    return tag + 1;
}
Exemplo n.º 3
0
/* returns 0 on error */
static int run_let_string(struct VarSet *varset, const char *rd, struct Variable *v)
{
	char *str, *strt;
	int len,n;

	rd = getstring(varset, rd, &len, &strt);
	str = malloc(len+1);
	if(str == NULL)
		outofmemory();

	strncpy(str, strt, len);
	str[len] = '\0';
	rd = skipwhite_s(rd);
	
	while(*rd == '+'){
		rd = skipwhite_s(rd+1);

		rd = getstring(varset, rd, &n, &strt);
		str = realloc(str, len += n);
		strncat(str, strt, n);
		rd = skipwhite_s(rd);
	}

	if(v->val_s != NULL)
		free(v->val_s);
	v->val_s = str;

	return 1;
}
Exemplo n.º 4
0
void *doMemAlloc(int size)
{
  void *ptr = NULL;
  ptr = malloc(size);
  if (!ptr) { outofmemory(); }
  return(ptr);
}
Exemplo n.º 5
0
/*
 * MyMalloc - allocate memory, call outofmemory on failure
 */
void *
MyMalloc(size_t size)
{
	void *ret = calloc(1, size);
	if(ret == NULL)
		outofmemory();
	return ret;
}
Exemplo n.º 6
0
void *
_BlockHeapAlloc(BlockHeap * bh)
{
    Block *walker;
    dlink_node *new_node;

    assert(bh != NULL);
    if (bh == NULL)
      {
        return(NULL);
      }

    if (bh->freeElems == 0)
      {   
        /* Allocate new block and assign */
        /* newblock returns 1 if unsuccessful, 0 if not */

        if (newblock(bh))
	  {
            /* That didn't work..try to garbage collect */
            BlockHeapGarbageCollect(bh);  
            if(bh->freeElems == 0)
              {
                 outofmemory(); /* Well that didn't work either...bail */
              }
	  }
       }
      
    for (walker = bh->base; walker != NULL; walker = walker->next)
      {
        if (walker->freeElems > 0)
	  {
            bh->freeElems--;
            walker->freeElems--;
            new_node = walker->free_list.head;
            dlinkDelete(new_node, &walker->free_list);
            dlinkAdd(new_node->data, new_node, &walker->used_list);
            assert(new_node->data != NULL);
            if(new_node->data == NULL)
              outofmemory();
            return (new_node->data);
	  }
      }
    assert(0 == 1);
    return(NULL);     /* If you get here, something bad happened ! */
}
Exemplo n.º 7
0
void initBlockHeap(void)
{
    zero_fd = open("/dev/zero", O_RDWR);
    if (zero_fd < 0)
    {
      outofmemory();
    }
}
Exemplo n.º 8
0
/*
 * MyRealloc - reallocate memory, call outofmemory on failure
 */
void* MyRealloc(void* x, size_t y)
{
  char *ret = realloc(x, y);

  if (!ret)
    outofmemory();
  return ret;
}
Exemplo n.º 9
0
/*
 * MyMalloc - allocate memory, call outofmemory on failure
 */
void* MyMalloc(size_t x)
{
  void* ret = malloc(x);

  if (!ret)
    outofmemory();
  return ret;
}
Exemplo n.º 10
0
void
initBlockHeap(void)
{
  zero_fd = open("/dev/zero", O_RDWR);

  if (zero_fd < 0)
    outofmemory();
  fd_open(zero_fd, FD_FILE, "Anonymous mmap()");
}
Exemplo n.º 11
0
/*
 * MyRealloc - reallocate memory, call outofmemory on failure
 */
void *
MyRealloc(void *x, size_t y)
{
	void *ret = realloc(x, y);

	if(ret == NULL)
		outofmemory();
	return ret;
}
Exemplo n.º 12
0
/**
   Does the actual reading of the MOTD. To be called only by
   read_motd() or read_motd_asynch_downloaded().
 */
void do_read_motd(const char *filename, aMotdFile *themotd)
{
	FILE *fd;
	struct tm *tm_tmp;
	time_t modtime;

	char line[512];
	char *tmp;

	aMotdLine *last, *temp;

	free_motd(themotd);

	if(!filename)
		return;

	fd = fopen(filename, "r");
	if (!fd)
		return;

	/* record file modification time */
	modtime = unreal_getfilemodtime(filename);
	tm_tmp = localtime(&modtime);
	memcpy(&themotd->last_modified, tm_tmp, sizeof(struct tm));

	last = NULL;
	while (fgets(line, sizeof(line), fd))
	{
		if ((tmp = strchr(line, '\n')))
			*tmp = '\0';
		if ((tmp = strchr(line, '\r')))
			*tmp = '\0';
		if (strlen(line) > MOTD_LINE_LEN)
			line[MOTD_LINE_LEN] = '\0';

		temp = MyMalloc(sizeof(aMotdLine));
		if (!temp)
			outofmemory();
		AllocCpy(temp->line, line);

		if(last)
			last->next = temp;
		else
			/* handle the special case of the first line */
			themotd->lines = temp;

		last = temp;
	}
	/* the file could be zero bytes long? */
	if(last)
		last->next = NULL;

	fclose(fd);
	
	return;
}
Exemplo n.º 13
0
void *MyRealloc(void *x, size_t y)
{
    void       *ret = realloc(x, y);

    if (!ret)
    {
	outofmemory();
    }
    return ret;
}
Exemplo n.º 14
0
/*
** Create a new aClient structure and set it to initial state.
**
**	from == NULL,	create local client (a client connected
**			to a socket).
**
**	from,	create remote client (behind a socket
**			associated with the client defined by
**			'from'). ('from' is a local client!!).
*/
aClient	*make_client(aClient *from)
{
	Reg	aClient *cptr = NULL;
	Reg	unsigned size = CLIENT_REMOTE_SIZE;

	/*
	 * Check freelists first to see if we can grab a client without
	 * having to call malloc.
	 */
	if (!from)
		size = CLIENT_LOCAL_SIZE;

	if (!(cptr = (aClient *)MyMalloc(size)))
		outofmemory();
	bzero((char *)cptr, (int)size);

#ifdef	DEBUGMODE
	if (size == CLIENT_LOCAL_SIZE)
		cloc.inuse++;
	else
		crem.inuse++;
#endif

	/* Note:  structure is zero (calloc) */
	cptr->from = from ? from : cptr; /* 'from' of local client is self! */
	cptr->next = NULL; /* For machines with NON-ZERO NULL pointers >;) */
	cptr->prev = NULL;
	cptr->hnext = NULL;
	cptr->user = NULL;
	cptr->serv = NULL;
	cptr->name = cptr->namebuf;
	cptr->status = STAT_UNKNOWN;
	cptr->fd = -1;
	(void)strcpy(cptr->username, "unknown");
	cptr->info = DefInfo;
	if (size == CLIENT_LOCAL_SIZE)
	    {
		cptr->since = cptr->lasttime = cptr->firsttime = timeofday;
		cptr->confs = NULL;
		cptr->sockhost[0] = '\0';
		cptr->buffer[0] = '\0';
		cptr->authfd = -1;
		cptr->auth = cptr->username;
		cptr->exitc = EXITC_UNDEF;
		cptr->receiveB = cptr->sendB = cptr->receiveM = cptr->sendM = 0;
#ifdef	ZIP_LINKS
		cptr->zip = NULL;
#endif
#ifdef XLINE
		cptr->user2 = NULL;
		cptr->user3 = NULL;
#endif
	    }
	return (cptr);
}
Exemplo n.º 15
0
/* ************************************************************************ */
BlockHeap * BlockHeapCreate (size_t elemsize,
                     int elemsperblock)
{
   BlockHeap *bh;

   /* Catch idiotic requests up front */
   if ((elemsize <= 0) || (elemsperblock <= 0))
     {
       outofmemory();   /* die.. out of memory */
     }

   /* Allocate our new BlockHeap */
   bh = (BlockHeap *) MyMalloc( sizeof (BlockHeap));
   if (bh == NULL) 
     {
       outofmemory(); /* die.. out of memory */
     }

   elemsize = elemsize + (elemsize & (sizeof(void *) - 1));
   bh->elemSize = elemsize;
   bh->elemsPerBlock = elemsperblock;
   bh->blocksAllocated = 0;
   bh->freeElems = 0;
   bh->numlongs = (bh->elemsPerBlock / (sizeof(long) * 8)) + 1;
   if ( (bh->elemsPerBlock % (sizeof(long) * 8)) == 0)
     bh->numlongs--;
   bh->base = NULL;

   /* Be sure our malloc was successful */
   if (newblock(bh))
     {
       free(bh);
       outofmemory(); /* die.. out of memory */
     }
   /* DEBUG */
   if(bh == NULL)
     {
       outofmemory(); /* die.. out of memory */
     }

   return bh;
}
Exemplo n.º 16
0
/*! \brief Returns a pointer to a struct within our BlockHeap that's free for
 *         the taking.
 * \param bh Pointer to the Blockheap
 * \return Address pointer to allocated data space, or NULL if unsuccessful
 */
void *
BlockHeapAlloc(BlockHeap *bh)
{
  Block *walker = NULL;
  dlink_node *new_node = NULL;

  assert(bh != NULL);

  if (bh->freeElems == 0)
  {   
    /* Allocate new block and assign */
    /* newblock returns 1 if unsuccessful, 0 if not */
    if (newblock(bh))
    {
      /* That didn't work..try to garbage collect */
      BlockHeapGarbageCollect(bh);  

      if (newblock(bh))
        outofmemory(); /* Well that didn't work either...bail */
    }
  }
      
  for (walker = bh->base; walker != NULL; walker = walker->next)
  {
    if (walker->freeElems > 0)
    {
      --bh->freeElems;
      --walker->freeElems;
      new_node = walker->free_list.head;

      dlinkDelete(new_node, &walker->free_list);
      assert(new_node->data != NULL);

      memset(new_node->data, 0, bh->elemSize);
      return new_node->data;
    }
  }

  assert(0 == 1);
  outofmemory();
  return NULL;
}
Exemplo n.º 17
0
Link *make_link()
{
  Link  *lp;

  lp = MyMalloc(sizeof(Link));
  if( lp == (Link *)NULL)
    outofmemory();

  lp->next = (Link *)NULL;              /* just to be paranoid... */

  return lp;
}
Exemplo n.º 18
0
void *
xstrdup(const char *s)
{
  void *ret = malloc(strlen(s) + 1);

  if (ret == NULL)
    outofmemory();

  strcpy(ret, s);

  return ret;
}
Exemplo n.º 19
0
void *
xstrndup(const char *s, size_t len)
{
  void *ret = malloc(len + 1);

  if (ret == NULL)
    outofmemory();

  strlcpy(ret, s, len + 1);

  return ret;
}
Exemplo n.º 20
0
char* getstring(void)
{
	char *buf;
	int maxlen = 50, len = 0;
	
	buf = malloc(maxlen);
	if(buf == NULL)
		outofmemory();

	while(look != '\r' && look != '\n'){
		if(len == maxlen){
			buf = realloc(buf, maxlen += 50);
			if(buf == NULL)
				outofmemory();
			
		}
		buf[len++] = look;
		getlook();
	}
	
	return buf;
}
Exemplo n.º 21
0
/*! \brief Creates a new blockheap
 *
 * Creates a new blockheap from which smaller blocks can be allocated.
 * Intended to be used instead of multiple calls to malloc() when
 * performance is an issue.
 *
 * \param name          Name of the blockheap
 * \param elemsize      Size of the basic element to be stored
 * \param elemsperblock Number of elements to be stored in a single block of
 *                      memory.  When the blockheap runs out of free memory,
 *                      it will allocate elemsize * elemsperblock more.
 * \return Pointer to new BlockHeap, or NULL if unsuccessful
 */
BlockHeap *
BlockHeapCreate(const char *const name, size_t elemsize, int elemsperblock)
{
  BlockHeap *bh = NULL;
  assert(elemsize > 0 && elemsperblock > 0);

  /* Catch idiotic requests up front */
  if ((elemsize <= 0) || (elemsperblock <= 0))
    outofmemory();    /* die.. out of memory */

  /* Allocate our new BlockHeap */
  if ((bh = calloc(1, sizeof(BlockHeap))) == NULL)
    outofmemory();    /* die.. out of memory */

  if ((elemsize % sizeof(void *)) != 0)
  {
    /* Pad to even pointer boundary */
    elemsize += sizeof(void *);
    elemsize &= ~(sizeof(void *) - 1);
  }

  bh->name = name;
  bh->elemSize = elemsize;
  bh->elemsPerBlock = elemsperblock;

  /* Be sure our malloc was successful */
  if (newblock(bh))
  {
    if (bh != NULL)
      free(bh);

     outofmemory();    /* die.. out of memory */
  }

  bh->next = heap_list;
  heap_list = bh;

  return bh;
}
Exemplo n.º 22
0
/*! \brief Opens /dev/zero and saves the file handle for
 * future allocations.
 */
void
initBlockHeap(void)
{
#ifdef HAVE_MMAP
#ifndef MAP_ANON
  int zero_fd = open("/dev/zero", O_RDWR);

  if (zero_fd < 0)
    outofmemory();
  fd_open(&dpfd, zero_fd, 0, "Anonymous mmap()");
#endif
  eventAdd("heap_garbage_collection", &heap_garbage_collection, NULL, 119);
#endif
}
Exemplo n.º 23
0
/** Read motd-like file, used for rules/motd/botmotd/opermotd/etc.
 *  Multiplexes to either directly reading the MOTD or downloading it asynchronously.
 * @param filename Filename of file to read or URL. NULL is accepted and causes the *motd to be free()d.
 * @param motd Reference to motd pointer (used for freeing if needed and for asynchronous remote MOTD support)
 */
void read_motd(const char *filename, aMotdFile *themotd)
{
#ifdef USE_LIBCURL
	time_t modtime;
	aMotdDownload *motd_download;
#endif

	/* TODO: if themotd points to a tld's motd,
	   could a rehash disrupt this pointer?*/
#ifdef USE_LIBCURL
	if(themotd->motd_download)
	{
		themotd->motd_download->themotd = NULL;
		/*
		 * It is not our job to free() motd_download, the
		 * read_motd_asynch_downloaded() function will do that
		 * when it sees that ->themod == NULL.
		 */
		themotd->motd_download = NULL;
	}

	/* if filename is NULL, do_read_motd will catch it */
	if(filename && url_is_valid(filename))
	{
		/* prepare our payload for read_motd_asynch_downloaded() */
		motd_download = MyMallocEx(sizeof(aMotdDownload));
		if(!motd_download)
			outofmemory();
		motd_download->themotd = themotd;
		themotd->motd_download = motd_download;

#ifdef REMOTEINC_SPECIALCACHE
		modtime = unreal_getfilemodtime(unreal_mkcache(filename));
#else
		modtime = 0;
#endif

		download_file_async(filename, modtime, (vFP)read_motd_asynch_downloaded, motd_download);
		return;
	}
#endif /* USE_LIBCURL */

	do_read_motd(filename, themotd);

	return;
}
Exemplo n.º 24
0
Arquivo: table.c Projeto: 8l/FUZIX
PUBLIC void syminit()
{
    unsigned i;

    for (i = sizeof(int) <= 2 ? 0xE000 : (unsigned) 0x38000;
	 i != 0; i -= 512)
	if ((tableptr = malloc(i)) != NUL_PTR)
	    break;
    if (tableptr == NUL_PTR)
	outofmemory();
    tableend = tableptr + i;
    for (i = 0; i < HASHTABSIZE; i++)
	hashtab[i] = NUL_PTR;

    mainavail = tableend - tableptr;
    usedtop = 0;
}
Exemplo n.º 25
0
/* ************************************************************************ */
int
_BlockHeapFree(BlockHeap * bh, void *ptr)
{
    Block *block;
    struct MemBlock *memblock;
    
    assert(bh != NULL);
    assert(ptr != NULL);

    if (bh == NULL)
      {

        ilog(L_NOTICE, "balloc.c:BlockHeapFree() bh == NULL");
        return(1);
      }

    if (ptr == NULL)
      {
        ilog(L_NOTICE, "balloc.BlockHeapFree() ptr == NULL");
        return(1);
      }

    memblock = (void *)((size_t)ptr - sizeof(MemBlock));
    assert(memblock->block != NULL);
    if(memblock->block == NULL)
    {
      outofmemory();
    }
    /* Is this block really on the used list? */
    assert(dlinkFind(&memblock->block->used_list, memblock) == NULL); 

    block = memblock->block;
    bh->freeElems++;
    block->freeElems++;
    mem_frob(ptr, bh->elemSize);
    dlinkDelete(&memblock->self, &block->used_list);
    dlinkAdd(ptr, &memblock->self, &block->free_list);
    return(0);
}
Exemplo n.º 26
0
/*
** 'make_user' add's an User information block to a client
** if it was not previously allocated.
*/
anUser* make_user(aClient *cptr)
{
  anUser        *user;

  user = cptr->user;
  if (!user)
    {
      user = MyMalloc(sizeof(anUser));
      if( user == (anUser *)NULL)
        outofmemory();
      user->away = NULL;
      user->server = (char *)NULL;      /* scache server name */
      user->refcnt = 1;
      user->joined = 0;
      user->channel = NULL;
      user->invited = NULL;
      user->silence = NULL;
      user->vlink = NULL;
      cptr->user = user;
    }
  return user;
}
Exemplo n.º 27
0
void *MyMalloc_impl(MemTracer *mt, size_t mlen)
{
    MemTag *tag;

    if (!mt->initialized)
    {
        mt->next = mtrace_list;
        mtrace_list = mt;
        mt->initialized = 1;
    }

    tag = malloc(mlen + sizeof(MemTag));

    if (!tag)
        outofmemory();

    tag->tracer = mt;
    tag->length = mlen;
    mt->objects++;
    mt->allocated += mlen;

    return tag + 1;
}
Exemplo n.º 28
0
/*! \brief Returns an element to the free pool, does not free()
 * \param bh  Pointer to BlockHeap containing element
 * \param ptr Pointer to element to be "freed"
 * \return 0 if successful, 1 if element not contained within BlockHeap
 */
int
BlockHeapFree(BlockHeap *bh, void *ptr)
{
  Block *block = NULL;
  struct MemBlock *memblock = NULL;
    
  assert(bh != NULL);
  assert(ptr != NULL);

  memblock = (void *)((size_t)ptr - sizeof(MemBlock));
  assert(memblock->block != NULL);

  if (memblock->block == NULL)
    outofmemory();

  block = memblock->block;
  ++bh->freeElems;
  ++block->freeElems;
  mem_frob(ptr, bh->elemSize);

  dlinkAdd(ptr, &memblock->self, &block->free_list);
  return 0;
}