🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: vmsnet.internals
Article #361

Re: malloc() as a user defined function

#361
From: Dave Schaumann
Date: Sun, 11 Oct 1992 05:47
59 lines
2622 bytes
In article <Bvv6pH.Gzy@cs.psu.edu>, ytang@red (Yuan-Ling Tang) writes:
>    In programming on dynamic memory allocation, I prefer doing check on
>    every malloc(). I.e.:
>
>    if ((p=(SOME_TYPE *)malloc(sizeof(SOME_TYPE))) == NULL) {
>	printf("Malloc error.\n");
>	exit(0);
>    }
>
>    It turned out it's a pain to type in the above four lines if I have 
>    lots of malloc() invocations in the program, also, the program looks
>    ugly. So I tried to use a function called my_malloc():
>
>    SOME_TYPE *my_malloc()

[deleted, coded as above]
>
>    [...] what if I have many distinct types of memory space to allocate?

Here's an idea I cribbed off of a compiler I worked on last semester (the
source is up for ftp, so it should be OK to use & distribute freely):

Somewhere, I have the following function:

	void *mustalloc( size_t size ) {
	  void *t = malloc(size) ;
	  if( t == NULL ) FATAL( "malloc returns NULL" ) ;
	  return t ;
	  }

FATAL(), of course, is just a simple print 'n' exit routine, which I made
a seperate subroutine since such a thing is often genrally useful in a
large program.


Along with it, I have the following macros:

	extern void *mustalloc( size_t ) ;

	#define  new(t  ) ((t *)mustalloc( sizeof(t) ))
	#define anew(t,s) ((t *)mustalloc( sizeof(t) * (s) ))

With this, I can write code like

	struct foo *p = new(struct FOO) ;
	struct foo foo_array = anew(struct FOO, 100) ;

This provides the advantages of a clean interface to a properly typed
dynamic allocation routine, while hiding the ugly details.  An added
benefit is that if you do all your allocation through mustalloc(), you
automatically have a place to put malloc() wrapper code if you need to
do malloc()/free() debugging (or a place to do garbage collection, if
you prefer).

I've been using this pretty much since I saw it.  IMHO, it works really
great, and I would encourage others to try it too.

-- 
Caught an internal error--.brainrc restored
Technical Details
Message-ID: <1992Oct11.054708.26380@organpipe.uug.arizona.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!gatech!ncar!noao!amethyst!organpipe.uug.arizona.edu!news
References: <Bvv6pH.Gzy@cs.psu.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!sparky!uunet!gatech!ncar!noao!amethyst!organpipe.uug.arizona.edu!news
From: dave@cs.arizona.edu (Dave Schaumann)
Newsgroups: vmsnet.internals,vmsnet.misc,comp.lang.c
Subject: Re: malloc() as a user defined function
Summary: a new() idea
Message-ID: <1992Oct11.054708.26380@organpipe.uug.arizona.edu>
Date: 11 Oct 92 05:47:08 GMT
References: <Bvv6pH.Gzy@cs.psu.edu>
Sender: news@organpipe.uug.arizona.edu
Reply-To: dave@cs.arizona.edu (Dave Schaumann)
Followup-To: vmsnet.internals
Organization: University of Arizona
Lines: 59
In-Reply-To: ytang@red.crayola.cs.psu.edu (Yuan-Ling Tang)