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
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
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
|
|
|
|
1995-03-23 03:40:21 +00:00
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this library; if not, write to the Free
|
2002-12-31 19:31:46 +00:00
|
|
|
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
|
|
|
|
|
|
|
|
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
|
1995-03-23 03:40:21 +00:00
|
|
|
|
2000-10-31 16:17:33 +00:00
|
|
|
typedef struct _NSZone NSZone;
|
|
|
|
|
|
|
|
#include <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
|
|
|
|
1997-01-06 21:35:52 +00:00
|
|
|
|
1997-03-03 19:58:17 +00:00
|
|
|
/* The members are the same as the structure mstats which is in the
|
|
|
|
GNU C library. */
|
|
|
|
struct NSZoneStats
|
|
|
|
{
|
|
|
|
size_t bytes_total;
|
|
|
|
size_t chunks_used;
|
|
|
|
size_t bytes_used;
|
|
|
|
size_t chunks_free;
|
|
|
|
size_t bytes_free;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
1999-01-28 17:21:03 +00:00
|
|
|
void *GSOutOfMemory(size_t size, BOOL retry);
|
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
#ifdef IN_NSZONE_M
|
|
|
|
#define GS_ZONE_SCOPE extern
|
|
|
|
#define GS_ZONE_ATTR
|
|
|
|
#else
|
|
|
|
#define GS_ZONE_SCOPE static inline
|
|
|
|
#define GS_ZONE_ATTR __attribute__((unused))
|
|
|
|
#endif
|
|
|
|
|
1999-04-12 12:53:30 +00:00
|
|
|
/* Default zone. Name is hopelessly long so that no one will ever
|
|
|
|
want to use it. ;) Private variable. */
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT NSZone* __nszone_private_hidden_default_zone;
|
1999-04-12 12:53:30 +00:00
|
|
|
|
1999-01-28 17:21:03 +00:00
|
|
|
#ifndef GS_WITH_GC
|
|
|
|
#define GS_WITH_GC 0
|
|
|
|
#endif
|
|
|
|
#if GS_WITH_GC
|
|
|
|
|
|
|
|
#include <gc.h>
|
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT NSZone* __nszone_private_hidden_atomic_zone;
|
1999-09-28 10:25:42 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE NSZone* NSCreateZone (size_t start, size_t gran, BOOL canFree)
|
1999-04-12 12:53:30 +00:00
|
|
|
{ return __nszone_private_hidden_default_zone; }
|
1999-01-28 17:21:03 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE NSZone* NSDefaultMallocZone (void)
|
1999-04-12 12:53:30 +00:00
|
|
|
{ return __nszone_private_hidden_default_zone; }
|
1999-01-28 17:21:03 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE NSZone* GSAtomicMallocZone (void)
|
1999-09-28 19:35:09 +00:00
|
|
|
{ return __nszone_private_hidden_atomic_zone; }
|
1999-09-28 10:25:42 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE NSZone* NSZoneFromPointer (void *ptr)
|
1999-04-12 12:53:30 +00:00
|
|
|
{ return __nszone_private_hidden_default_zone; }
|
1999-01-28 17:21:03 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void* NSZoneMalloc (NSZone *zone, size_t size)
|
1999-01-28 17:21:03 +00:00
|
|
|
{
|
1999-09-28 10:25:42 +00:00
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
if (zone == GSAtomicMallocZone())
|
1999-09-28 19:35:09 +00:00
|
|
|
ptr = (void*)GC_MALLOC_ATOMIC(size);
|
1999-09-28 10:25:42 +00:00
|
|
|
else
|
1999-09-28 19:35:09 +00:00
|
|
|
ptr = (void*)GC_MALLOC(size);
|
1999-01-28 17:21:03 +00:00
|
|
|
|
|
|
|
if (ptr == 0)
|
|
|
|
ptr = GSOutOfMemory(size, YES);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void* NSZoneCalloc (NSZone *zone, size_t elems, size_t bytes)
|
1999-01-28 17:21:03 +00:00
|
|
|
{
|
|
|
|
size_t size = elems * bytes;
|
1999-09-28 10:25:42 +00:00
|
|
|
void *ptr;
|
|
|
|
|
|
|
|
if (zone == __nszone_private_hidden_atomic_zone)
|
1999-09-28 19:35:09 +00:00
|
|
|
ptr = (void*)GC_MALLOC_ATOMIC(size);
|
1999-09-28 10:25:42 +00:00
|
|
|
else
|
1999-09-28 19:35:09 +00:00
|
|
|
ptr = (void*)GC_MALLOC(size);
|
1999-01-28 17:21:03 +00:00
|
|
|
|
|
|
|
if (ptr == 0)
|
1999-09-28 19:35:09 +00:00
|
|
|
ptr = GSOutOfMemory(size, NO);
|
1999-01-28 17:21:03 +00:00
|
|
|
memset(ptr, '\0', size);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void* NSZoneRealloc (NSZone *zone, void *ptr, size_t size)
|
1999-01-28 17:21:03 +00:00
|
|
|
{
|
|
|
|
ptr = GC_REALLOC(ptr, size);
|
|
|
|
if (ptr == 0)
|
|
|
|
GSOutOfMemory(size, NO);
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void NSRecycleZone (NSZone *zone)
|
1999-01-28 17:21:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void NSZoneFree (NSZone *zone, void *ptr)
|
1999-01-28 17:21:03 +00:00
|
|
|
{
|
|
|
|
GC_FREE(ptr);
|
|
|
|
}
|
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void NSSetZoneName (NSZone *zone, NSString *name)
|
1999-01-28 17:21:03 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE NSString* NSZoneName (NSZone *zone)
|
1999-01-28 17:21:03 +00:00
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
1999-05-21 15:21:41 +00:00
|
|
|
#ifndef NO_GNUSTEP
|
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void* NSZoneMallocAtomic (NSZone *zone, size_t size)
|
1999-05-21 15:21:41 +00:00
|
|
|
{
|
1999-09-28 19:35:09 +00:00
|
|
|
return NSZoneMalloc(GSAtomicMallocZone(), size);
|
1999-05-21 15:21:41 +00:00
|
|
|
}
|
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE BOOL NSZoneCheck (NSZone *zone)
|
1999-01-28 17:21:03 +00:00
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE struct NSZoneStats NSZoneStats (NSZone *zone)
|
1999-01-28 17:21:03 +00:00
|
|
|
{
|
|
|
|
struct NSZoneStats stats = { 0 };
|
|
|
|
return stats;
|
|
|
|
}
|
1999-05-21 15:21:41 +00:00
|
|
|
#endif
|
1999-01-28 17:21:03 +00:00
|
|
|
|
|
|
|
#else /* GS_WITH_GC */
|
1996-03-22 02:35:21 +00:00
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT NSZone* NSCreateZone (size_t start, size_t gran, BOOL canFree);
|
1995-03-23 03:40:21 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE NSZone* NSDefaultMallocZone (void) GS_ZONE_ATTR;
|
|
|
|
|
|
|
|
GS_ZONE_SCOPE NSZone* NSDefaultMallocZone (void)
|
1999-05-21 15:21:41 +00:00
|
|
|
{
|
|
|
|
return __nszone_private_hidden_default_zone;
|
|
|
|
}
|
1995-03-23 03:40:21 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE NSZone* GSAtomicMallocZone (void) GS_ZONE_ATTR;
|
|
|
|
|
|
|
|
GS_ZONE_SCOPE NSZone* GSAtomicMallocZone (void)
|
1999-09-28 10:25:42 +00:00
|
|
|
{
|
|
|
|
return NSDefaultMallocZone();
|
|
|
|
}
|
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT NSZone* NSZoneFromPointer (void *ptr);
|
1995-03-23 03:40:21 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void* NSZoneMalloc (NSZone *zone, size_t size) GS_ZONE_ATTR;
|
|
|
|
|
|
|
|
GS_ZONE_SCOPE void* NSZoneMalloc (NSZone *zone, size_t size)
|
1999-05-21 15:21:41 +00:00
|
|
|
{
|
|
|
|
if (!zone)
|
|
|
|
zone = NSDefaultMallocZone();
|
|
|
|
return (zone->malloc)(zone, size);
|
|
|
|
}
|
1996-03-22 02:35:21 +00:00
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT void* NSZoneCalloc (NSZone *zone, size_t elems, size_t bytes);
|
1996-05-28 20:47:33 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void*
|
|
|
|
NSZoneRealloc (NSZone *zone, void *ptr, size_t size) GS_ZONE_ATTR;
|
|
|
|
|
|
|
|
GS_ZONE_SCOPE void* NSZoneRealloc (NSZone *zone, void *ptr, size_t size)
|
1999-05-21 15:21:41 +00:00
|
|
|
{
|
|
|
|
if (!zone)
|
|
|
|
zone = NSDefaultMallocZone();
|
|
|
|
return (zone->realloc)(zone, ptr, size);
|
|
|
|
}
|
1997-01-06 21:35:52 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void NSRecycleZone (NSZone *zone) GS_ZONE_ATTR;
|
|
|
|
|
|
|
|
GS_ZONE_SCOPE void NSRecycleZone (NSZone *zone)
|
1999-05-21 15:21:41 +00:00
|
|
|
{
|
|
|
|
if (!zone)
|
|
|
|
zone = NSDefaultMallocZone();
|
|
|
|
(zone->recycle)(zone);
|
|
|
|
}
|
1997-01-06 21:35:52 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void NSZoneFree (NSZone *zone, void *ptr) GS_ZONE_ATTR;
|
|
|
|
|
|
|
|
GS_ZONE_SCOPE void NSZoneFree (NSZone *zone, void *ptr)
|
1999-05-21 15:21:41 +00:00
|
|
|
{
|
|
|
|
if (!zone)
|
|
|
|
zone = NSDefaultMallocZone();
|
|
|
|
(zone->free)(zone, ptr);
|
|
|
|
}
|
1996-05-28 20:47:33 +00:00
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT void NSSetZoneName (NSZone *zone, NSString *name);
|
1996-05-28 20:47:33 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE NSString* NSZoneName (NSZone *zone) GS_ZONE_ATTR;
|
|
|
|
|
|
|
|
GS_ZONE_SCOPE NSString* NSZoneName (NSZone *zone)
|
1999-05-21 15:21:41 +00:00
|
|
|
{
|
|
|
|
if (!zone)
|
|
|
|
zone = NSDefaultMallocZone();
|
|
|
|
return zone->name;
|
|
|
|
}
|
1996-05-28 20:47:33 +00:00
|
|
|
|
1999-05-21 15:21:41 +00:00
|
|
|
#ifndef NO_GNUSTEP
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE void*
|
|
|
|
NSZoneMallocAtomic (NSZone *zone, size_t size) GS_ZONE_ATTR;
|
|
|
|
|
|
|
|
GS_ZONE_SCOPE void* NSZoneMallocAtomic (NSZone *zone, size_t size)
|
1999-05-21 15:21:41 +00:00
|
|
|
{
|
|
|
|
if (!zone)
|
|
|
|
zone = NSDefaultMallocZone();
|
|
|
|
return (zone->malloc)(zone, size);
|
|
|
|
}
|
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE BOOL NSZoneCheck (NSZone *zone) GS_ZONE_ATTR;
|
|
|
|
|
|
|
|
GS_ZONE_SCOPE BOOL NSZoneCheck (NSZone *zone)
|
1999-05-21 15:21:41 +00:00
|
|
|
{
|
|
|
|
if (!zone)
|
|
|
|
zone = NSDefaultMallocZone();
|
|
|
|
return (zone->check)(zone);
|
|
|
|
}
|
1997-03-03 19:58:17 +00:00
|
|
|
|
2000-06-22 03:15:27 +00:00
|
|
|
GS_ZONE_SCOPE struct NSZoneStats NSZoneStats (NSZone *zone) GS_ZONE_ATTR;
|
|
|
|
|
|
|
|
GS_ZONE_SCOPE struct NSZoneStats NSZoneStats (NSZone *zone)
|
1999-05-21 15:21:41 +00:00
|
|
|
{
|
|
|
|
if (!zone)
|
|
|
|
zone = NSDefaultMallocZone();
|
|
|
|
return (zone->stats)(zone);
|
|
|
|
}
|
|
|
|
#endif /* NO_GNUSTEP */
|
1997-03-03 19:58:17 +00:00
|
|
|
|
1999-01-28 17:21:03 +00:00
|
|
|
#endif /* GS_WITH_GC */
|
|
|
|
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT unsigned NSPageSize (void) __attribute__ ((const));
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT unsigned NSLogPageSize (void) __attribute__ ((const));
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT unsigned NSRoundDownToMultipleOfPageSize (unsigned bytes)
|
1999-11-18 15:18:47 +00:00
|
|
|
__attribute__ ((const));
|
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT unsigned NSRoundUpToMultipleOfPageSize (unsigned bytes)
|
1999-11-18 15:18:47 +00:00
|
|
|
__attribute__ ((const));
|
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT unsigned NSRealMemoryAvailable (void);
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT void* NSAllocateMemoryPages (unsigned bytes);
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT void NSDeallocateMemoryPages (void *ptr, unsigned bytes);
|
1999-11-18 15:18:47 +00:00
|
|
|
|
2000-06-14 04:03:56 +00:00
|
|
|
GS_EXPORT void NSCopyMemoryPages (const void *src, void *dest, unsigned bytes);
|
1999-11-18 15:18:47 +00:00
|
|
|
|
1997-03-03 19:43:25 +00:00
|
|
|
#endif /* not __NSZone_h_GNUSTEP_BASE_INCLUDE */
|