home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / extensions / lib / PEX / include / alloc.h next >
Encoding:
C/C++ Source or Header  |  1991-02-15  |  3.2 KB  |  98 lines

  1. /* $XConsortium: alloc.h,v 5.1 91/02/16 09:48:56 rws Exp $ */
  2.  
  3. /***********************************************************
  4. Copyright 1989, 1990, 1991 by Sun Microsystems, Inc. and the X Consortium.
  5.  
  6.                         All Rights Reserved
  7.  
  8. Permission to use, copy, modify, and distribute this software and its 
  9. documentation for any purpose and without fee is hereby granted, 
  10. provided that the above copyright notice appear in all copies and that
  11. both that copyright notice and this permission notice appear in 
  12. supporting documentation, and that the names of Sun Microsystems,
  13. the X Consortium, and MIT not be used in advertising or publicity 
  14. pertaining to distribution of the software without specific, written 
  15. prior permission.  
  16.  
  17. SUN MICROSYSTEMS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 
  18. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT 
  19. SHALL SUN MICROSYSTEMS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 
  20. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  22. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  23. SOFTWARE.
  24. ******************************************************************/
  25.  
  26. /* Assures that allocated memory is freed when *alloc is unsuccessful */
  27.  
  28. #ifndef ALLOC_INCLUDED
  29. #define ALLOC_INCLUDED
  30.  
  31. /* Use this macro to declare as many holders for malloc'd pointers as needed. */
  32. /* There is no error checking for exceeding the array size, so be generous. */
  33. #define    ALLOC_DECLARE(max)                    \
  34.         char    * _allocated[(max)];            \
  35.         char    **_alloc_ptr = _allocated    /* caller will ; */
  36. /* _alloc_ptr points AFTER the last allocated (successful) pointer.    */
  37.  
  38. /* After each call to malloc/realloc, merely:    ALLOCATED(var);     */
  39. #define    ALLOCATED(ptr)        (*_alloc_ptr++ = (char*)(ptr))
  40.  
  41. /* To test the 1 most recent ALLOCATED failed:    if (ALLOC_FAILED)    */
  42. #define    ALLOC_FAILED        ( *(_alloc_ptr - 1) == NULL )
  43.  
  44. /* In case of failure, merely:            ALLOC_FREE;        */
  45. #define    ALLOC_FREE    while (--_alloc_ptr >= _allocated)    \
  46.                 if (*_alloc_ptr)            \
  47.                 free(*_alloc_ptr)    /* caller will ; */
  48. /* **RESTRICTION** if realloc moves the data & returns a new pointer,
  49.  *           then this new pointer must be freed, and the old pointer
  50.  *           must NOT be freed.  This means that ALLOC_FREE can't
  51.  *           be used for this pointer.
  52.  */
  53.  
  54. #define    ALLOC_RESET        (_alloc_ptr = _allocated)
  55.  
  56. #ifdef EXAMPLE_USES
  57. /* The following shows several different ways to use these macros.  */
  58.  
  59. #define SUCCESS        1
  60. #define FAILURE        0
  61. int
  62. routine()
  63. {
  64.     struct  s1    {
  65.     int    a, b;
  66.     }        *ptr1;
  67.     double    *ptr2;
  68.     int     *ptr3;
  69.     ALLOC_DECLARE(10);
  70.  
  71.     ptr1 = (struct s1 *) malloc((unsigned)100 * sizeof(struct s1));
  72.     if (! ALLOCATED(ptr1)) {
  73.     ALLOC_FREE;
  74.     return FAILURE;
  75.     }
  76.  
  77.     ALLOCATED( ptr2 = (double *) malloc((unsigned) 200) );
  78.     if (ALLOC_FAILED) {
  79.     ALLOC_FREE;
  80.     return FAILURE;
  81.     }
  82.  
  83.     if (! ALLOCATED(ptr3 = (int *) malloc((unsigned) 300))) {
  84.     ALLOC_FREE;
  85.     return FAILURE;
  86.     }
  87.  
  88.     /* . . . Use ptr1 and ptr2 and ptr3 . . . */
  89.  
  90.     /* Free the variables that are no longer needed */
  91.     free( (char*)ptr2);
  92.     free( (char*)ptr3 );
  93.     return SUCCESS;
  94. }
  95. #endif /* EXAMPLE_USES */
  96.  
  97. #endif /* ALLOC_INCLUDED */
  98.