2002-12-31 19:31:46 +00:00
|
|
|
/** Zone memory management. -*- Mode: ObjC -*-
|
1999-09-28 10:25:42 +00:00
|
|
|
Copyright (C) 1997,1998,1999 Free Software Foundation, Inc.
|
1995-07-01 19:01:11 +00:00
|
|
|
|
1997-05-03 18:15:44 +00:00
|
|
|
Written by: Yoo C. Chung <wacko@laplace.snu.ac.kr>
|
1997-03-03 19:43:25 +00:00
|
|
|
Date: January 1997
|
|
|
|
|
1996-05-12 00:56:10 +00:00
|
|
|
This file is part of the GNUstep Base Library.
|
1995-03-23 03:40:21 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1995-03-23 03:40:21 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
1997-03-03 19:43:25 +00:00
|
|
|
|
1995-03-23 03:40:21 +00:00
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
1997-03-03 19:43:25 +00:00
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1995-03-23 03:40:21 +00:00
|
|
|
License along with this library; if not, write to the Free
|
2006-10-31 07:05:46 +00:00
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
2002-12-31 19:31:46 +00:00
|
|
|
|
|
|
|
AutogsdocSource: NSZone.m
|
|
|
|
AutogsdocSource: NSPage.m
|
|
|
|
|
|
|
|
*/
|
1995-03-23 03:40:21 +00:00
|
|
|
|
1997-01-06 21:35:52 +00:00
|
|
|
#ifndef __NSZone_h_GNUSTEP_BASE_INCLUDE
|
|
|
|
#define __NSZone_h_GNUSTEP_BASE_INCLUDE
|
2006-10-31 07:05:46 +00:00
|
|
|
#import <GNUstepBase/GSVersionMacros.h>
|
1995-03-23 03:40:21 +00:00
|
|
|
|
2004-07-29 15:30:47 +00:00
|
|
|
/**
|
|
|
|
* Primary structure representing an <code>NSZone</code>. Technically it
|
|
|
|
* consists of a set of function pointers for zone upkeep functions plus some
|
|
|
|
* other things-
|
|
|
|
<example>
|
|
|
|
{
|
|
|
|
// Functions for zone.
|
|
|
|
void *(*malloc)(struct _NSZone *zone, size_t size);
|
|
|
|
void *(*realloc)(struct _NSZone *zone, void *ptr, size_t size);
|
|
|
|
void (*free)(struct _NSZone *zone, void *ptr);
|
|
|
|
void (*recycle)(struct _NSZone *zone);
|
|
|
|
BOOL (*check)(struct _NSZone *zone);
|
|
|
|
BOOL (*lookup)(struct _NSZone *zone, void *ptr);
|
|
|
|
|
|
|
|
// Zone statistics (not always maintained).
|
|
|
|
struct NSZoneStats (*stats)(struct _NSZone *zone);
|
|
|
|
|
|
|
|
size_t gran; // Zone granularity (passed in on initialization)
|
|
|
|
NSString *name; // Name of zone (default is 'nil')
|
|
|
|
NSZone *next; // Pointer used for internal management of multiple zones.
|
|
|
|
}</example>
|
|
|
|
*/
|
2000-10-31 16:17:33 +00:00
|
|
|
typedef struct _NSZone NSZone;
|
|
|
|
|
2006-10-31 07:05:46 +00:00
|
|
|
#import <Foundation/NSObjCRuntime.h>
|
1996-10-31 17:22:04 +00:00
|
|
|
|
1997-01-06 21:35:52 +00:00
|
|
|
@class NSString;
|
1996-05-28 20:47:33 +00:00
|
|
|
|
2006-09-13 10:20:49 +00:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
1997-01-06 21:35:52 +00:00
|
|
|
struct _NSZone
|
|
|
|
{
|
1997-03-03 19:43:25 +00:00
|
|
|
/* Functions for zone. */
|
|
|
|
void *(*malloc)(struct _NSZone *zone, size_t size);
|
|
|
|
void *(*realloc)(struct _NSZone *zone, void *ptr, size_t size);
|
|
|
|
void (*free)(struct _NSZone *zone, void *ptr);
|
|
|
|
void (*recycle)(struct _NSZone *zone);
|
1997-03-03 19:58:17 +00:00
|
|
|
BOOL (*check)(struct _NSZone *zone);
|
1998-10-15 05:03:16 +00:00
|
|
|
BOOL (*lookup)(struct _NSZone *zone, void *ptr);
|
1997-03-03 19:58:17 +00:00
|
|
|
struct NSZoneStats (*stats)(struct _NSZone *zone);
|
|
|
|
|
1997-03-03 19:43:25 +00:00
|
|
|
size_t gran; // Zone granularity
|
|
|
|
NSString *name; // Name of zone (default is 'nil')
|
1998-10-15 05:03:16 +00:00
|
|
|
NSZone *next;
|
1997-01-06 21:35:52 +00:00
|
|
|
};
|
|
|
|
|
2009-04-15 08:03:19 +00:00
|
|
|
/**
|
|
|
|
* Creates a new zone of start bytes, which will grow and shrink by
|
|
|
|
* granularity bytes. If canFree is 0, memory in zone is allocated but
|
|
|
|
* never freed, meaning allocation will be very fast. The whole zone can
|
|
|
|
* still be freed with NSRecycleZone(), and you should still call NSZoneFree
|
|
|
|
* on memory in the zone that is no longer needed, since a count of allocated
|
|
|
|
* pointers is kept and must reach zero before freeing the zone.<br />
|
|
|
|
* If Garbage Collection is enabled, this function does nothing other than
|
|
|
|
* log a warning and return the same value as the NSDefaultMallocZone()
|
|
|
|
* function.
|
|
|
|
*/
|
2009-01-20 10:15:52 +00:00
|
|
|
GS_EXPORT NSZone*
|
2009-02-23 20:42:32 +00:00
|
|
|
NSCreateZone (NSUInteger start, NSUInteger gran, BOOL canFree);
|
1996-03-22 02:35:21 +00:00
|
|
|
|
2009-04-15 08:03:19 +00:00
|
|
|
/** Returns the default zone for memory allocation. Memory created in this
|
|
|
|
* zone is the same as memory allocates using the system malloc() function.
|
|
|
|
*/
|
2009-01-20 10:15:52 +00:00
|
|
|
GS_EXPORT NSZone*
|
2009-01-19 11:00:33 +00:00
|
|
|
NSDefaultMallocZone (void);
|
2000-06-22 03:15:27 +00:00
|
|
|
|
2009-04-15 08:03:19 +00:00
|
|
|
/**
|
|
|
|
* Searches and finds the zone ptr was allocated from. The speed depends
|
|
|
|
* upon the number of zones and their size.<br />
|
|
|
|
* If Garbage Collection is enabled, this function always returns the
|
|
|
|
* same as the NSDefaultMallocZone() function.
|
|
|
|
*/
|
2009-01-20 10:15:52 +00:00
|
|
|
GS_EXPORT NSZone*
|
2009-01-19 11:00:33 +00:00
|
|
|
NSZoneFromPointer (void *ptr);
|
2000-06-22 03:15:27 +00:00
|
|
|
|
2004-07-29 15:30:47 +00:00
|
|
|
/**
|
2009-04-15 08:03:19 +00:00
|
|
|
* Allocates and returns memory for elems items of size bytes, in the
|
|
|
|
* given zone. Returns NULL if allocation of size 0 requested. Raises
|
|
|
|
* <code>NSMallocException</code> if not enough free memory in zone to
|
|
|
|
* allocate and no more can be obtained from system, unless using the
|
2009-04-26 05:37:21 +00:00
|
|
|
* default zone, in which case NULL is returned.<br />
|
2009-04-15 08:03:19 +00:00
|
|
|
* If Garbage Collection is enabled, this function always allocates
|
|
|
|
* non-scanned, non-collectable memory in the NSDefaultMallocZone() and
|
|
|
|
* the zone argument is ignored.
|
2004-07-29 15:30:47 +00:00
|
|
|
*/
|
2009-01-20 10:15:52 +00:00
|
|
|
GS_EXPORT void*
|
2009-02-23 20:42:32 +00:00
|
|
|
NSZoneMalloc (NSZone *zone, NSUInteger size);
|
1996-03-22 02:35:21 +00:00
|
|
|
|
2004-07-29 15:30:47 +00:00
|
|
|
/**
|
2009-04-15 08:03:19 +00:00
|
|
|
* Allocates and returns cleared memory for elems items of size bytes, in the
|
|
|
|
* given zone. Returns NULL if allocation of size 0 requested. Raises
|
|
|
|
* <code>NSMallocException</code> if not enough free memory in zone to
|
|
|
|
* allocate and no more can be obtained from system, unless using the
|
|
|
|
* default zone, in which case NULL is returned.<br />
|
|
|
|
* If Garbage Collection is enabled, this function always allocates
|
|
|
|
* non-scanned, non-collectable memory in the NSDefaultMallocZone() and
|
|
|
|
* the zone argument is ignored.
|
2004-07-29 15:30:47 +00:00
|
|
|
*/
|
2009-01-20 10:15:52 +00:00
|
|
|
GS_EXPORT void*
|
2009-02-23 20:42:32 +00:00
|
|
|
NSZoneCalloc (NSZone *zone, NSUInteger elems, NSUInteger bytes);
|
2000-06-22 03:15:27 +00:00
|
|
|
|
2004-07-29 15:30:47 +00:00
|
|
|
/**
|
2009-04-15 08:03:19 +00:00
|
|
|
* Reallocates the chunk of memory in zone pointed to by ptr to a new one of
|
|
|
|
* size bytes. Existing contents in ptr are copied over. Raises an
|
|
|
|
* <code>NSMallocException</code> if insufficient memory is available in the
|
|
|
|
* zone and no more memory can be obtained from the system, unless using the
|
|
|
|
* default zone, in which case NULL is returned.<br />
|
|
|
|
* If Garbage Collection is enabled, the zone argument is ignored.
|
2004-07-29 15:30:47 +00:00
|
|
|
*/
|
2009-01-20 10:15:52 +00:00
|
|
|
GS_EXPORT void*
|
2009-02-23 20:42:32 +00:00
|
|
|
NSZoneRealloc (NSZone *zone, void *ptr, NSUInteger size);
|
2000-06-22 03:15:27 +00:00
|
|
|
|
2004-07-29 15:30:47 +00:00
|
|
|
/**
|
|
|
|
* Return memory for an entire zone to system. In fact, this will not be done
|
|
|
|
* unless all memory in the zone has been explicitly freed (by calls to
|
2005-11-06 13:53:40 +00:00
|
|
|
* NSZoneFree()). For "non-freeable" zones, the number of NSZoneFree() calls
|
2004-07-29 15:30:47 +00:00
|
|
|
* must simply equal the number of allocation calls. The default zone, on the
|
2009-04-15 08:03:19 +00:00
|
|
|
* other hand, cannot be recycled.<br />
|
|
|
|
* If Garbage Collection is enabled, this function has not effect.
|
2004-07-29 15:30:47 +00:00
|
|
|
*/
|
2009-01-20 10:15:52 +00:00
|
|
|
GS_EXPORT void
|
2009-01-19 11:00:33 +00:00
|
|
|
NSRecycleZone (NSZone *zone);
|
2000-06-22 03:15:27 +00:00
|
|
|
|
2004-07-29 15:30:47 +00:00
|
|
|
/**
|
|
|
|
* Frees memory pointed to by ptr (which should have been allocated by a
|
|
|
|
* previous call to NSZoneMalloc(), NSZoneCalloc(), or NSZoneRealloc()) and
|
|
|
|
* returns it to zone. Note, if this is a nonfreeable zone, the memory is
|
2009-04-15 08:03:19 +00:00
|
|
|
* not actually freed, but the count of number of free()s is updated.<br />
|
|
|
|
* If Garbage Collection is enabled, the zone argument is ignored and this
|
|
|
|
* function causes ptr to be deallocated immediately.
|
2004-07-29 15:30:47 +00:00
|
|
|
*/
|
2009-01-20 10:15:52 +00:00
|
|
|
GS_EXPORT void
|
2009-01-19 11:00:33 +00:00
|
|
|
NSZoneFree (NSZone *zone, void *ptr);
|
2000-06-22 03:15:27 +00:00
|
|
|
|
2004-07-29 15:30:47 +00:00
|
|
|
/**
|
2009-01-19 11:00:33 +00:00
|
|
|
* Sets name of the given zone (useful for debugging and logging).
|
2004-07-29 15:30:47 +00:00
|
|
|
*/
|
2009-01-20 10:15:52 +00:00
|
|
|
GS_EXPORT void
|
2009-01-19 11:00:33 +00:00
|
|
|
NSSetZoneName (NSZone *zone, NSString *name);
|
2000-06-22 03:15:27 +00:00
|
|
|
|
2004-07-29 15:30:47 +00:00
|
|
|
/**
|
2009-04-15 08:03:19 +00:00
|
|
|
* Returns the name of the given zone (useful for debugging and logging).
|
2004-07-29 15:30:47 +00:00
|
|
|
*/
|
2009-01-20 10:15:52 +00:00
|
|
|
GS_EXPORT NSString*
|
2009-01-19 11:00:33 +00:00
|
|
|
NSZoneName (NSZone *zone);
|
1999-05-21 15:21:41 +00:00
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
#if OS_API_VERSION(GS_API_NONE, GS_API_NONE)
|
2000-06-22 03:15:27 +00:00
|
|
|
|
2009-01-20 10:15:52 +00:00
|
|
|
/** Deprecated ...<br />
|
2004-07-29 15:30:47 +00:00
|
|
|
* Checks integrity of a zone. Not defined by OpenStep or OS X.
|
|
|
|
*/
|
2009-01-19 11:00:33 +00:00
|
|
|
BOOL
|
|
|
|
NSZoneCheck (NSZone *zone);
|
2000-06-22 03:15:27 +00:00
|
|
|
|
2004-07-29 15:30:47 +00:00
|
|
|
/**
|
2009-01-20 10:15:52 +00:00
|
|
|
* <code>NSZoneStats</code> is the structure returned by the NSZoneStats()
|
|
|
|
* function that summarizes the current usage of a zone. It is similar to
|
|
|
|
* the structure <em>mstats</em> in the GNU C library. It has 5 fields of
|
|
|
|
* type <code>size_t</code>-
|
|
|
|
* <deflist>
|
|
|
|
* <term><code>bytes_total</code></term>
|
|
|
|
* <desc>
|
|
|
|
* This is the total size of memory managed by the zone, in bytes.</desc>
|
|
|
|
* <term><code>chunks_used</code></term>
|
|
|
|
* <desc>This is the number of memory chunks in use in the zone.</desc>
|
|
|
|
* <term><code>bytes_used</code></term>
|
|
|
|
* <desc>This is the number of bytes in use.</desc>
|
|
|
|
* <term><code>chunks_free</code></term>
|
|
|
|
* <desc>This is the number of memory chunks that are not in use.</desc>
|
|
|
|
* <term><code>bytes_free</code></term>
|
|
|
|
* <desc>
|
|
|
|
* This is the number of bytes managed by the zone that are not in use.
|
|
|
|
* </desc>
|
|
|
|
* </deflist>
|
|
|
|
*/
|
|
|
|
struct NSZoneStats
|
|
|
|
{
|
|
|
|
size_t bytes_total;
|
|
|
|
size_t chunks_used;
|
|
|
|
size_t bytes_used;
|
|
|
|
size_t chunks_free;
|
|
|
|
size_t bytes_free;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Deprecated ...<br />
|
2004-07-29 15:30:47 +00:00
|
|
|
* Obtain statistics about the zone. Implementation emphasis is on
|
|
|
|
* correctness, not speed. Not defined by OpenStep or OS X.
|
|
|
|
*/
|
2009-01-19 11:00:33 +00:00
|
|
|
struct NSZoneStats
|
|
|
|
NSZoneStats (NSZone *zone);
|
|
|
|
|
2009-01-20 10:15:52 +00:00
|
|
|
/**
|
|
|
|
* Try to get more memory - the normal process has failed.
|
|
|
|
* If we can't do anything, just return a null pointer.
|
|
|
|
* Try to do some logging if possible.
|
|
|
|
*/
|
|
|
|
void*
|
2009-02-23 20:42:32 +00:00
|
|
|
GSOutOfMemory(NSUInteger size, BOOL retry);
|
2009-01-20 10:15:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called during +initialize to tell the class that instances created
|
|
|
|
* in future should have the specified instance variable as a weak
|
|
|
|
* pointer for garbage collection.<br />
|
|
|
|
* NB. making a pointer weak does not mean that it is automatically
|
|
|
|
* zeroed when the object it points to is garbage collected. To get that
|
|
|
|
* behavior you must asign values to the pointer using the
|
2009-02-09 16:16:11 +00:00
|
|
|
* GSAssignZeroingWeakPointer() function.<br />
|
2009-01-20 10:15:52 +00:00
|
|
|
* This function has no effect if the system is
|
|
|
|
* not built for garbage collection.
|
|
|
|
*/
|
|
|
|
GS_EXPORT void
|
2009-02-09 16:49:23 +00:00
|
|
|
GSMakeWeakPointer(Class theClass, const char *iVarName);
|
2009-01-20 10:15:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function must be used to assign a value to a zeroing weak pointer.<br />
|
|
|
|
* A zeroing weak pointer is one where, when the garbage collector collects
|
|
|
|
* the object pointed to, it also clears the weak pointer.<br />
|
|
|
|
* Assigning zero (nil) will always succeed and has the effect of telling the
|
|
|
|
* garbage collector that it no longer needs to track the previously assigned
|
|
|
|
* object. Apart from that case, a source needs to be garbage collectable for
|
|
|
|
* this function to work, and using a non-garbage collectable value will
|
|
|
|
* cause the function to return NO.<br />
|
2009-01-20 11:41:41 +00:00
|
|
|
* If the destination object (the weak pointer watching the source object)
|
|
|
|
* belongs to a chunk of memory which may be collected before the source
|
|
|
|
* object is collected, it is important that it is finalised and the
|
|
|
|
* finalisation code assigns zero to the pointer.<br />
|
2009-01-20 10:15:52 +00:00
|
|
|
* If garbage collection is not in use, this function performs a simple
|
|
|
|
* assignment returning YES, unless destination is null in which case it
|
|
|
|
* returns NO.
|
|
|
|
*/
|
|
|
|
GS_EXPORT BOOL
|
|
|
|
GSAssignZeroingWeakPointer(void **destination, void *source);
|
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
GS_EXPORT NSUInteger
|
|
|
|
NSPageSize (void) __attribute__ ((const));
|
1997-03-03 19:58:17 +00:00
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
GS_EXPORT NSUInteger
|
|
|
|
NSLogPageSize (void) __attribute__ ((const));
|
1999-01-28 17:21:03 +00:00
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
GS_EXPORT NSUInteger
|
|
|
|
NSRoundDownToMultipleOfPageSize (NSUInteger bytes) __attribute__ ((const));
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
GS_EXPORT NSUInteger
|
|
|
|
NSRoundUpToMultipleOfPageSize (NSUInteger bytes) __attribute__ ((const));
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
GS_EXPORT NSUInteger
|
|
|
|
NSRealMemoryAvailable (void);
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
GS_EXPORT void*
|
|
|
|
NSAllocateMemoryPages (NSUInteger bytes);
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
GS_EXPORT void
|
|
|
|
NSDeallocateMemoryPages (void *ptr, NSUInteger bytes);
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
GS_EXPORT void
|
|
|
|
NSCopyMemoryPages (const void *src, void *dest, NSUInteger bytes);
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
#if OS_API_VERSION(MAC_OS_X_VERSION_10_4, OS_API_LATEST)
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
enum {
|
|
|
|
NSScannedOption = (1<<0),
|
|
|
|
NSCollectorDisabledOption = (1<<1),
|
|
|
|
};
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2009-01-19 11:00:33 +00:00
|
|
|
/** Allocate memory. If garbage collection is not enabled this uses the
|
|
|
|
* default malloc zone and the options are ignored.<br />
|
|
|
|
* If garbage collection is enabled, the allocate memory is normally not
|
2009-03-09 15:11:51 +00:00
|
|
|
* scanned for pointers but is itsself garbage collectable. The options
|
2009-01-19 11:00:33 +00:00
|
|
|
* argument is a bitmask in which NSScannedOption sets the memory to be
|
|
|
|
* scanned for pointers by the garbage collector, and
|
|
|
|
* NSCollectorDisabledOption causes the memory to be excempt from being
|
2009-03-09 15:11:51 +00:00
|
|
|
* garbage collected itsself.<br />
|
|
|
|
* In any case the memory returned is zero'ed.
|
2009-01-19 11:00:33 +00:00
|
|
|
*/
|
|
|
|
GS_EXPORT void *
|
|
|
|
NSAllocateCollectable(NSUInteger size, NSUInteger options);
|
|
|
|
|
|
|
|
/** Reallocate memory to be of a different size and/or to have different
|
|
|
|
* options settings. The behavior of options is as for
|
|
|
|
* the NSAllocateCollectable() function.
|
|
|
|
*/
|
|
|
|
GS_EXPORT void *
|
|
|
|
NSReallocateCollectable(void *ptr, NSUInteger size, NSUInteger options);
|
|
|
|
|
|
|
|
#endif
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2006-09-13 10:20:49 +00:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1997-03-03 19:43:25 +00:00
|
|
|
#endif /* not __NSZone_h_GNUSTEP_BASE_INCLUDE */
|