mirror of
https://github.com/gnustep/libs-base.git
synced 2025-04-23 00:41:02 +00:00
Add methods from 10.8 for using zeroing weak references in collections.
git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@35877 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
0fd06dc66f
commit
38c34ba6ea
8 changed files with 110 additions and 10 deletions
|
@ -52,7 +52,9 @@ enum {
|
|||
NSHashTableCopyIn
|
||||
= NSPointerFunctionsCopyIn,
|
||||
NSHashTableObjectPointerPersonality
|
||||
= NSPointerFunctionsObjectPointerPersonality
|
||||
= NSPointerFunctionsObjectPointerPersonality,
|
||||
NSHashTableWeakMemory
|
||||
= NSPointerFunctionsWeakMemory
|
||||
};
|
||||
|
||||
typedef NSUInteger NSHashTableOptions;
|
||||
|
@ -62,6 +64,14 @@ typedef NSUInteger NSHashTableOptions;
|
|||
+ (id) hashTableWithOptions: (NSPointerFunctionsOptions)options;
|
||||
|
||||
+ (id) hashTableWithWeakObjects;
|
||||
/**
|
||||
* Creates a hash table that uses zeroing weak references (either using the
|
||||
* automatic reference counting or garbage collection mechanism, depending on
|
||||
* which mode this framework is compiled in) so that objects are removed when
|
||||
* their last other reference disappears.
|
||||
*/
|
||||
+ (id) weakObjectsHashTable;
|
||||
|
||||
|
||||
- (id) initWithOptions: (NSPointerFunctionsOptions)options
|
||||
capacity: (NSUInteger)initialCapacity;
|
||||
|
|
|
@ -52,7 +52,9 @@ enum {
|
|||
NSMapTableCopyIn
|
||||
= NSPointerFunctionsCopyIn,
|
||||
NSMapTableObjectPointerPersonality
|
||||
= NSPointerFunctionsObjectPointerPersonality
|
||||
= NSPointerFunctionsObjectPointerPersonality,
|
||||
NSMapTableWeakMemory
|
||||
= NSPointerFunctionsWeakMemory
|
||||
};
|
||||
|
||||
typedef NSUInteger NSMapTableOptions;
|
||||
|
@ -85,6 +87,27 @@ typedef NSUInteger NSMapTableOptions;
|
|||
*/
|
||||
+ (id) mapTableWithWeakToWeakObjects;
|
||||
|
||||
/** Convenience method for creating a map table to store object values
|
||||
* using object keys. The collection will retain both the key and the value.
|
||||
*/
|
||||
+ (id) strongToStrongObjectsMapTable;
|
||||
/** Convenience method for creating a map table to store object values
|
||||
* using object keys. The collection will retain the key, the value will be a
|
||||
* zeroing weak reference.
|
||||
*/
|
||||
+ (id) strongToWeakObjectsMapTable;
|
||||
/** Convenience method for creating a map table to store object values
|
||||
* using object keys. The collection will retain the value, the key will be a
|
||||
* zeroing weak reference.
|
||||
*/
|
||||
+ (id) weakToStrongObjectsMapTable;
|
||||
/** Convenience method for creating a map table to store object values
|
||||
* using object keys. The collection will use zeroing weak references for both
|
||||
* the key and the value.
|
||||
*/
|
||||
+ (id) weakToWeakObjectsMapTable;
|
||||
|
||||
|
||||
/** Initialiser using option bitmasks to describe the keys and values.
|
||||
*/
|
||||
- (id) initWithKeyOptions: (NSPointerFunctionsOptions)keyOptions
|
||||
|
|
|
@ -55,6 +55,15 @@ extern "C" {
|
|||
*/
|
||||
+ (id) pointerArrayWithPointerFunctions: (NSPointerFunctions *)functions;
|
||||
|
||||
/** Returns a new pointer array for storing strong (retained) references to
|
||||
* objects.
|
||||
*/
|
||||
+ (id) strongObjectsPointerArray;
|
||||
/** Returns a new pointer array for storing zeroing weak references to objects.
|
||||
*/
|
||||
+ (id) weakObjectsPointerArray;
|
||||
|
||||
|
||||
/** Removes all nil/zero items from the array.
|
||||
*/
|
||||
- (void) compact; // eliminate NULLs
|
||||
|
|
|
@ -56,6 +56,12 @@ enum {
|
|||
/** MACH virtual memory (not implemented) */
|
||||
NSPointerFunctionsMachVirtualMemory = (4<<0),
|
||||
|
||||
/** Uses read and write barriers appropriate for either automatic reference
|
||||
* counting or garbage collection. Note that the GNUstep implementation will
|
||||
* behave correctly when used with manual or automatic reference counting, as
|
||||
* long as the Objective-C runtime supports automatic reference counting.
|
||||
*/
|
||||
NSPointerFunctionsWeakMemory = (5<<0),
|
||||
|
||||
/** Use the -hash and -isEqual: methods for storing objects, and the
|
||||
* -description method to describe them. */
|
||||
|
|
|
@ -40,13 +40,8 @@
|
|||
# define STRONG_ACQUIRE(x) x
|
||||
#elif defined(OBJC_CAP_ARC)
|
||||
# include <objc/objc-arc.h>
|
||||
/*
|
||||
* OS X 10.7 uses weak to mean unsafe unretained, which is stupid and wrong,
|
||||
* but we should probably do the same thing for now. Uncomment this when Apple
|
||||
* fixes their implementation.
|
||||
# define WEAK_READ(x) objc_loadWeak((id*)x)
|
||||
# define WEAK_WRITE(addr, x) objc_storeWeak((id*)addr, (id)x)
|
||||
*/
|
||||
# define ARC_WEAK_READ(x) objc_loadWeak((id*)x)
|
||||
# define ARC_WEAK_WRITE(addr, x) objc_storeWeak((id*)addr, (id)x)
|
||||
# define WEAK_READ(x) (*x)
|
||||
# define WEAK_WRITE(addr, x) (*(addr) = x)
|
||||
# define STRONG_WRITE(addr, x) objc_storeStrong((id*)addr, (id)x)
|
||||
|
@ -61,6 +56,12 @@
|
|||
# define STRONG_WRITE(addr, x) ASSIGN(*((id*)addr), ((id)x))
|
||||
# define STRONG_ACQUIRE(x) RETAIN(((id)x))
|
||||
#endif
|
||||
#ifndef ARC_WEAK_WRITE
|
||||
# define ARC_WEAK_WRITE(addr, x) WEAK_WRITE(addr, x)
|
||||
#endif
|
||||
#ifndef ARC_WEAK_READ
|
||||
# define ARC_WEAK_READ(x) WEAK_READ(x)
|
||||
#endif
|
||||
|
||||
|
||||
/* Declare a structure type to copy pointer functions information
|
||||
|
@ -107,7 +108,11 @@ typedef struct
|
|||
*/
|
||||
static inline void *pointerFunctionsRead(PFInfo *PF, void **addr)
|
||||
{
|
||||
if (PF->options & NSPointerFunctionsZeroingWeakMemory)
|
||||
if (PF->options & NSPointerFunctionsWeakMemory)
|
||||
{
|
||||
return ARC_WEAK_READ((id*)addr);
|
||||
}
|
||||
else if (PF->options & NSPointerFunctionsZeroingWeakMemory)
|
||||
{
|
||||
return WEAK_READ((id*)addr);
|
||||
}
|
||||
|
@ -119,6 +124,10 @@ static inline void *pointerFunctionsRead(PFInfo *PF, void **addr)
|
|||
*/
|
||||
static inline void pointerFunctionsAssign(PFInfo *PF, void **addr, void *value)
|
||||
{
|
||||
if (PF->options & NSPointerFunctionsWeakMemory)
|
||||
{
|
||||
ARC_WEAK_WRITE(addr, value);
|
||||
}
|
||||
if (PF->options & NSPointerFunctionsZeroingWeakMemory)
|
||||
{
|
||||
WEAK_WRITE(addr, value);
|
||||
|
|
|
@ -71,6 +71,13 @@ static Class concreteClass = 0;
|
|||
return AUTORELEASE(t);
|
||||
}
|
||||
|
||||
+ (id) weakObjectsHashTable
|
||||
|
||||
{
|
||||
return [self hashTableWithOptions:
|
||||
NSPointerFunctionsObjectPersonality | NSPointerFunctionsWeakMemory];
|
||||
}
|
||||
|
||||
+ (id) hashTableWithWeakObjects
|
||||
{
|
||||
return [self hashTableWithOptions:
|
||||
|
|
|
@ -98,6 +98,31 @@ static Class concreteClass = 0;
|
|||
valueOptions: NSPointerFunctionsObjectPersonality
|
||||
| NSPointerFunctionsZeroingWeakMemory];
|
||||
}
|
||||
+ (id) strongToStrongObjectsMapTable
|
||||
{
|
||||
return [self mapTableWithKeyOptions: NSMapTableObjectPointerPersonality
|
||||
valueOptions: NSMapTableObjectPointerPersonality];
|
||||
}
|
||||
+ (id) strongToWeakObjectsMapTable
|
||||
{
|
||||
return [self mapTableWithKeyOptions: NSMapTableObjectPointerPersonality
|
||||
valueOptions: NSMapTableObjectPointerPersonality |
|
||||
NSMapTableWeakMemory];
|
||||
}
|
||||
+ (id) weakToStrongObjectsMapTable
|
||||
{
|
||||
return [self mapTableWithKeyOptions: NSMapTableObjectPointerPersonality |
|
||||
NSMapTableWeakMemory
|
||||
valueOptions: NSMapTableObjectPointerPersonality];
|
||||
}
|
||||
+ (id) weakToWeakObjectsMapTable
|
||||
{
|
||||
return [self mapTableWithKeyOptions: NSMapTableObjectPointerPersonality |
|
||||
NSMapTableWeakMemory
|
||||
valueOptions: NSMapTableObjectPointerPersonality |
|
||||
NSMapTableWeakMemory];
|
||||
}
|
||||
|
||||
|
||||
- (id) initWithKeyOptions: (NSPointerFunctionsOptions)keyOptions
|
||||
valueOptions: (NSPointerFunctionsOptions)valueOptions
|
||||
|
|
|
@ -79,6 +79,17 @@ static Class concreteClass = Nil;
|
|||
{
|
||||
return AUTORELEASE([[self alloc] initWithPointerFunctions: functions]);
|
||||
}
|
||||
+ (id) strongObjectsPointerArray
|
||||
{
|
||||
return [self pointerArrayWithOptions: NSPointerFunctionsObjectPersonality |
|
||||
NSPointerFunctionsStrongMemory];
|
||||
}
|
||||
+ (id) weakObjectsPointerArray
|
||||
{
|
||||
return [self pointerArrayWithOptions: NSPointerFunctionsObjectPersonality |
|
||||
NSPointerFunctionsWeakMemory];
|
||||
}
|
||||
|
||||
|
||||
- (void) compact
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue