home *** CD-ROM | disk | FTP | other *** search
- //
- // FREE.h -- useful macros
- // Written and copyright 1994, 1995, 1996 by Raf Schietekat.
- // Version 1995-03-11. All rights reserved.
- // This notice may not be removed from this source code.
- //
- // This code is included in the MiscKit by permission from the author
- // and its use is governed by the MiscKit license, found in the file
- // "LICENSE.rtf" in the MiscKit distribution. Please refer to that file
- // for a list of all applicable permissions and restrictions.
- //
-
- // These macros do not use the "MISC" prefix because that would ruin
- // their elegance. If they cause you grief, just #define MISC_SKIP_FREE
- // before importing the MiscBase.h or misckit.h...
-
- /*
- Author: Raf Schietekat (RfSchtkt@maze.ruca.ua.ac.be on 1995-03-11)
- Copyright: Raf Schietekat, 1994, 1995
- License:
- - You may use this code in your product without any fee, and you also don't
- have to mention your use of this code.
- - You may not change this file. If you want to, propose a change, and I
- may agree to it. My purpose is to have only one version
- of this code around, and to always have the best version :-).
- - You should provide this code to anyone who asks for it, for free,
- or covering the cost of the media only, and the recipient will be bound
- by the same licence terms.
- - This license does not limit my use of this code in any other way.
- License applications:
- - GNU projects may use this file as long as any changes are made only in
- the form of #undef/#define pairs external to this file, regardless of the
- general GNU license terms. This file does not become part of GNU, and
- the owner of the GNU license may not prohibit anyone from using this file
- in a private project.
- - Likewise, for the MiscKit in the NEXTSTEP world, the MiscKit administrator
- may at most prepend an introductory comment to this file, but he should not
- modify anything else. This file does not become part of the MiscKit
- (except to the extent that it will remain available), and the MiscKit's
- administrator may not prohibit anyone from extracting this file
- from the MiscKit, removing the introductory comment by the MiscKit's
- administrator, and using this file in a private project without
- recognising the MiscKit.
- - These are not really additions to the license, merely clarifications,
- and similar rules apply to anyone else who redistributes this file.
- Guarantee: every imagineable disclaimer applies.
- Version: 1995-03-11
- Maturity:
- Stable concept.
- You might like to know that the author uses these all the time.
- Acknowledgements: so far, only the author has contributed to this code
-
- These are macros to free things that (ultimately) rely on things allocated
- for them (called handles and resources here). The idea is to record in the
- handle itself whether or not the resource is still available, my means of a
- special value when it is not.
- Example: a pointer (the handle) to a memory region (the resource), which should
- either point to a valid memory region or be NULL; otherwise the pointer
- is said to dangle, and you might not immediately notice if you write
- though it, as opposed to writing through a NULL pointer, which (on many
- systems?) raises a signal.
- Use FREE_ if the resource *might* currently be allocated.
- Use FFREE_ (Forced Free) if you know that the resource is currently allocated.
- It's a tiny little bit faster, and you might by informed about wrong
- assumptions by an immediate failure. But the most useful
- property is that it is self-documenting: the author using these is saying
- ``look, I know I can get away with just FREE_, but I'm also telling you
- that I know that at this point this resource is still allocated''.
- The handle will be set to nil/NULL/-1, whichever is appropriate for the
- particular handle. You'll understand that this does not
- protect you against invalid accesses from copies of these handles,
- just to name one limitation, and the problem of reliable resource handling is
- an extended one. But these macros should be useful.
- */
-
-
- /********** ANSI **********/
-
- /* For malloc/free memory... */
- #define FFREE_MALLOC(h) ((void)(free(h),(h)=NULL))
- #define FREE_MALLOC(h) ((void)((h)?FFREE_MALLOC(h):0))
-
- /* For fopen() streams... */
- #define _FFREE_FILE_ERROR(h) \
- (printf("FFREE_FILE(%p) error in %s at line %i.\n",h,__FILE__,__LINE__))
- #define FFREE_FILE(h) ((void)((fclose(h)?_FFREE_FILE_ERROR(h):0),(h)=NULL))
- #define FREE_FILE(h) ((void)((h)?FFREE_FILE(h):0))
-
- /********** Unix **********/
-
- /* For Unix file descriptors... */
- #define FFREE_FD(h) ((void)(close(h),(h)=-1))
- #define FREE_FD(h) ((void)(((h)!=-1)?FFREE_FD(h):0))
-
- /* For popen() streams (use (F)FREE_FD for pipe())... */
- #define FFREE_PIPE(h) ((void)(pclose(h),(h)=NULL))
- #define FREE_PIPE(h) ((void)((h)?FFREE_PIPE(h):0))
-
-
- /********** Objective-C **********/
-
- /* For an Obj-C object, in situations where -free suffices... */
- #define FFREE_OBJECT(h) ((void)([(h) free],(h)=nil))
- #define FREE_OBJECT(h) ((void)((h)?FFREE_OBJECT(h):0))
-
- /* For an Obj-C List if you also want to free its contents... */
- #define FFREE_LIST(h) ((void)([(h) freeObjects],[(h) free],(h)=nil))
- #define FREE_LIST(h) ((void)((h)?FFREE_LIST(h):0))
-
-
- /********** NEXTSTEP/OpenStep **********/
-
- /* For NXStream streams... */
- #define FFREE_NXSTREAM(h) ((void)(NXClose(h),(h)=NULL))
- #define FREE_NXSTREAM(h) ((void)((h)?FFREE_NXSTREAM(h):0))
-
- /* For NXStream streams opened on memory that you don't want to preserve... */
- #define FFREE_MEMORYSTREAM(h) \
- ((void)(NXCloseMemory((h),NX_FREEBUFFER),(h)=NULL))
- #define FREE_MEMORYSTREAM(h) ((void)((h)?FFREE_MEMORYSTREAM(h):0))
-
- /* For NXTypedStream streams... */
- #define FFREE_NXTYPEDSTREAM(h) ((void)(NXCloseTypedStream(h),(h)=NULL))
- #define FREE_NXTYPEDSTREAM(h) ((void)((h)?FFREE_NXTYPEDSTREAM(h):0))
-
- /* For a Button connected to a PopUpList... */
- #define FFREE_POPUPLISTBUTTON(h) \
- ((void)([[(h) target] free],[(h) free],(h)=nil))
- #define FREE_POPUPLISTBUTTON(h) ((void)((h)?FFREE_POPUPLISTBUTTON(h):0))
-
- /* For an NXZone... */
- #define FFREE_NXZONE(h) ((void)(NXDestroyZone(h),(h)=NULL))
- #define FREE_NXZONE(h) ((void)((h)?FFREE_NXZONE(h):0))
-
- /* For an OpenStep object, in situations where -release suffices... */
- #define FFREE_NSOBJECT(h) ((void)([(h) release],(h)=nil))
- #define FREE_NSOBJECT(h) ((void)((h)?FFREE_NSOBJECT(h):0))
-
-
- /********** Obsolete **********/
-
- /* There is a Typedstream or TypedStream in the GNU libobjects Obj-C library */
- #define FREE_TYPEDSTREAM(h) Please_use_FREE_NXTYPEDSTREAM_instead...
- #define FFREE_TYPEDSTREAM(h) Please_use_FFREE_NXTYPEDSTREAM_instead...
-