2009-02-10 19:47:01 +00:00
|
|
|
/**Interface for NSConcretePointerFunctions for GNUStep
|
|
|
|
Copyright (C) 2009 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <rfm@gnu.org>
|
|
|
|
Date: 2009
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
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
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
2009-02-10 19:47:01 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
2019-12-09 23:36:00 +00:00
|
|
|
Boston, MA 02110 USA.
|
2009-02-10 19:47:01 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "Foundation/NSPointerFunctions.h"
|
2011-07-23 16:16:01 +00:00
|
|
|
|
|
|
|
#ifdef __GNUSTEP_RUNTIME__
|
|
|
|
# include <objc/capabilities.h>
|
|
|
|
#endif
|
|
|
|
|
2016-05-14 09:34:01 +00:00
|
|
|
#if defined(OBJC_CAP_ARC)
|
2011-07-23 16:16:01 +00:00
|
|
|
# include <objc/objc-arc.h>
|
2012-12-11 17:49:28 +00:00
|
|
|
# define ARC_WEAK_READ(x) objc_loadWeak((id*)x)
|
|
|
|
# define ARC_WEAK_WRITE(addr, x) objc_storeWeak((id*)addr, (id)x)
|
2011-08-03 09:04:12 +00:00
|
|
|
# define WEAK_READ(x) (*x)
|
|
|
|
# define WEAK_WRITE(addr, x) (*(addr) = x)
|
2011-07-23 16:16:01 +00:00
|
|
|
# define STRONG_WRITE(addr, x) objc_storeStrong((id*)addr, (id)x)
|
|
|
|
# define STRONG_ACQUIRE(x) objc_retain(x)
|
|
|
|
#else
|
|
|
|
# define WEAK_READ(x) (*x)
|
2016-03-25 11:15:28 +00:00
|
|
|
# define WEAK_WRITE(addr, x) (*(addr) = x)
|
2011-07-24 22:12:59 +00:00
|
|
|
# define STRONG_WRITE(addr, x) ASSIGN(*((id*)addr), ((id)x))
|
|
|
|
# define STRONG_ACQUIRE(x) RETAIN(((id)x))
|
2011-05-28 18:05:29 +00:00
|
|
|
#endif
|
2012-12-11 17:49:28 +00:00
|
|
|
#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
|
2009-02-10 19:47:01 +00:00
|
|
|
|
2011-07-23 16:16:01 +00:00
|
|
|
|
2009-03-16 10:54:59 +00:00
|
|
|
/* Declare a structure type to copy pointer functions information
|
2009-02-15 06:03:54 +00:00
|
|
|
* around easily.
|
|
|
|
*/
|
|
|
|
typedef struct
|
2009-02-10 19:47:01 +00:00
|
|
|
{
|
2009-02-15 06:03:54 +00:00
|
|
|
void* (*acquireFunction)(const void *item,
|
2009-02-10 19:47:01 +00:00
|
|
|
NSUInteger (*size)(const void *item), BOOL shouldCopy);
|
|
|
|
|
2009-02-15 06:03:54 +00:00
|
|
|
NSString *(*descriptionFunction)(const void *item);
|
2009-02-10 19:47:01 +00:00
|
|
|
|
2009-02-15 06:03:54 +00:00
|
|
|
NSUInteger (*hashFunction)(const void *item,
|
2009-02-10 19:47:01 +00:00
|
|
|
NSUInteger (*size)(const void *item));
|
|
|
|
|
2009-02-15 06:03:54 +00:00
|
|
|
BOOL (*isEqualFunction)(const void *item1, const void *item2,
|
2009-02-10 19:47:01 +00:00
|
|
|
NSUInteger (*size)(const void *item));
|
|
|
|
|
2009-02-15 06:03:54 +00:00
|
|
|
void (*relinquishFunction)(const void *item,
|
2009-02-10 19:47:01 +00:00
|
|
|
NSUInteger (*size)(const void *item));
|
|
|
|
|
2009-02-15 06:03:54 +00:00
|
|
|
NSUInteger (*sizeFunction)(const void *item);
|
|
|
|
|
2009-04-17 08:12:52 +00:00
|
|
|
NSPointerFunctionsOptions options;
|
2009-02-10 19:47:01 +00:00
|
|
|
|
2009-02-15 06:03:54 +00:00
|
|
|
} PFInfo;
|
2009-02-10 19:47:01 +00:00
|
|
|
|
2012-12-22 16:21:55 +00:00
|
|
|
inline static BOOL memoryType(int options, int flag)
|
|
|
|
{
|
2023-08-19 22:06:55 +00:00
|
|
|
return ((options & 0xff) == flag) ? YES : NO;
|
2012-12-22 16:21:55 +00:00
|
|
|
}
|
|
|
|
|
2023-08-18 15:29:43 +00:00
|
|
|
inline static BOOL personalityType(int options, int flag)
|
|
|
|
{
|
2023-08-19 22:06:55 +00:00
|
|
|
return ((options & 0xff00) == flag) ? YES : NO;
|
2023-08-18 15:29:43 +00:00
|
|
|
}
|
|
|
|
|
2009-02-15 06:03:54 +00:00
|
|
|
/* Declare the concrete pointer functions class as a wrapper around
|
|
|
|
* an instance of the PFInfo structure.
|
|
|
|
*/
|
|
|
|
@interface NSConcretePointerFunctions : NSPointerFunctions
|
|
|
|
{
|
|
|
|
@public
|
|
|
|
PFInfo _x;
|
2009-02-10 19:47:01 +00:00
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2009-02-14 18:18:26 +00:00
|
|
|
/* Wrapper functions to make use of the pointer functions.
|
2009-02-14 16:38:59 +00:00
|
|
|
*/
|
|
|
|
|
2011-05-28 18:05:29 +00:00
|
|
|
/**
|
|
|
|
* Reads the pointer from the specified address, inserting a read barrier if
|
|
|
|
* required.
|
|
|
|
*/
|
|
|
|
static inline void *pointerFunctionsRead(PFInfo *PF, void **addr)
|
|
|
|
{
|
2012-12-22 16:21:55 +00:00
|
|
|
if (memoryType(PF->options, NSPointerFunctionsWeakMemory))
|
2012-12-11 17:49:28 +00:00
|
|
|
{
|
|
|
|
return ARC_WEAK_READ((id*)addr);
|
|
|
|
}
|
2012-12-22 16:21:55 +00:00
|
|
|
if (memoryType(PF->options, NSPointerFunctionsZeroingWeakMemory))
|
2011-05-28 18:05:29 +00:00
|
|
|
{
|
2011-07-23 16:16:01 +00:00
|
|
|
return WEAK_READ((id*)addr);
|
2011-05-28 18:05:29 +00:00
|
|
|
}
|
|
|
|
return *addr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assigns a pointer, inserting the correct write barrier if required.
|
|
|
|
*/
|
|
|
|
static inline void pointerFunctionsAssign(PFInfo *PF, void **addr, void *value)
|
|
|
|
{
|
2012-12-22 16:21:55 +00:00
|
|
|
if (memoryType(PF->options, NSPointerFunctionsWeakMemory))
|
2012-12-11 17:49:28 +00:00
|
|
|
{
|
|
|
|
ARC_WEAK_WRITE(addr, value);
|
|
|
|
}
|
2012-12-22 16:21:55 +00:00
|
|
|
else if (memoryType(PF->options, NSPointerFunctionsZeroingWeakMemory))
|
2011-05-28 18:05:29 +00:00
|
|
|
{
|
2011-07-23 16:16:01 +00:00
|
|
|
WEAK_WRITE(addr, value);
|
|
|
|
}
|
2012-12-22 16:21:55 +00:00
|
|
|
else if (memoryType(PF->options, NSPointerFunctionsStrongMemory))
|
2011-07-23 16:16:01 +00:00
|
|
|
{
|
|
|
|
STRONG_WRITE(addr, value);
|
2011-05-28 18:05:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-07-23 16:16:01 +00:00
|
|
|
*addr = value;
|
2011-05-28 18:05:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-24 13:09:22 +00:00
|
|
|
static inline void *
|
2024-07-17 14:42:33 +00:00
|
|
|
pointerFunctionsAcquire(PFInfo *PF, void *src)
|
2011-07-24 13:09:22 +00:00
|
|
|
{
|
|
|
|
if (PF->acquireFunction != 0)
|
2024-07-17 14:42:33 +00:00
|
|
|
{
|
|
|
|
src = (*PF->acquireFunction)(src, PF->sizeFunction,
|
|
|
|
PF->options & NSPointerFunctionsCopyIn ? YES : NO);
|
|
|
|
}
|
2011-07-24 13:09:22 +00:00
|
|
|
return src;
|
|
|
|
}
|
|
|
|
|
2011-05-28 18:05:29 +00:00
|
|
|
/**
|
|
|
|
* Moves a pointer from location to another.
|
|
|
|
*/
|
|
|
|
static inline void pointerFunctionsMove(PFInfo *PF, void **new, void **old)
|
|
|
|
{
|
|
|
|
pointerFunctionsAssign(PF, new, pointerFunctionsRead(PF, old));
|
2009-02-14 18:18:26 +00:00
|
|
|
}
|
2009-02-14 16:38:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Generate an NSString description of the item
|
|
|
|
*/
|
2009-02-14 18:18:26 +00:00
|
|
|
static inline NSString *
|
2009-02-15 06:03:54 +00:00
|
|
|
pointerFunctionsDescribe(PFInfo *PF, void *item)
|
2009-02-14 18:18:26 +00:00
|
|
|
{
|
2009-02-15 06:03:54 +00:00
|
|
|
if (PF->descriptionFunction != 0)
|
|
|
|
return (*PF->descriptionFunction)(item);
|
2009-02-14 18:18:26 +00:00
|
|
|
return nil;
|
|
|
|
}
|
2009-02-14 16:38:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Generate the hash of the item
|
|
|
|
*/
|
2009-02-14 18:18:26 +00:00
|
|
|
static inline NSUInteger
|
2009-02-15 06:03:54 +00:00
|
|
|
pointerFunctionsHash(PFInfo *PF, void *item)
|
2009-02-14 18:18:26 +00:00
|
|
|
{
|
2009-02-15 06:03:54 +00:00
|
|
|
if (PF->hashFunction != 0)
|
|
|
|
return (*PF->hashFunction)(item, PF->sizeFunction);
|
2009-02-23 20:42:32 +00:00
|
|
|
return (NSUInteger)(uintptr_t)item;
|
2009-02-14 18:18:26 +00:00
|
|
|
}
|
2009-02-14 16:38:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Compare two items for equality
|
|
|
|
*/
|
2009-02-14 18:18:26 +00:00
|
|
|
static inline BOOL
|
2009-02-15 06:03:54 +00:00
|
|
|
pointerFunctionsEqual(PFInfo *PF, void *item1, void *item2)
|
2009-02-14 18:18:26 +00:00
|
|
|
{
|
2009-02-15 06:03:54 +00:00
|
|
|
if (PF->isEqualFunction != 0)
|
|
|
|
return (*PF->isEqualFunction)(item1, item2, PF->sizeFunction);
|
2009-02-14 18:18:26 +00:00
|
|
|
if (item1 == item2)
|
|
|
|
return YES;
|
|
|
|
return NO;
|
|
|
|
}
|
2009-02-14 16:38:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Relinquish the specified item and set it to zero.
|
|
|
|
*/
|
2009-02-14 18:18:26 +00:00
|
|
|
static inline void
|
2009-02-15 06:03:54 +00:00
|
|
|
pointerFunctionsRelinquish(PFInfo *PF, void **itemptr)
|
2009-02-14 18:18:26 +00:00
|
|
|
{
|
2011-07-23 16:16:01 +00:00
|
|
|
|
2009-02-15 06:03:54 +00:00
|
|
|
if (PF->relinquishFunction != 0)
|
|
|
|
(*PF->relinquishFunction)(*itemptr, PF->sizeFunction);
|
2012-12-22 16:21:55 +00:00
|
|
|
if (memoryType(PF->options, NSPointerFunctionsWeakMemory))
|
|
|
|
ARC_WEAK_WRITE(itemptr, 0);
|
|
|
|
else if (memoryType(PF->options, NSPointerFunctionsZeroingWeakMemory))
|
|
|
|
WEAK_WRITE(itemptr, (void*)0);
|
2009-02-14 18:18:26 +00:00
|
|
|
else
|
|
|
|
*itemptr = 0;
|
|
|
|
}
|
2009-02-14 16:38:59 +00:00
|
|
|
|
2009-02-15 07:13:54 +00:00
|
|
|
|
|
|
|
static inline void
|
|
|
|
pointerFunctionsReplace(PFInfo *PF, void **dst, void *src)
|
|
|
|
{
|
|
|
|
if (src != *dst)
|
|
|
|
{
|
|
|
|
if (PF->acquireFunction != 0)
|
2009-04-17 08:12:52 +00:00
|
|
|
src = (*PF->acquireFunction)(src, PF->sizeFunction,
|
|
|
|
PF->options & NSPointerFunctionsCopyIn ? YES : NO);
|
2009-02-15 07:13:54 +00:00
|
|
|
if (PF->relinquishFunction != 0)
|
|
|
|
(*PF->relinquishFunction)(*dst, PF->sizeFunction);
|
2012-12-22 16:21:55 +00:00
|
|
|
if (memoryType(PF->options, NSPointerFunctionsWeakMemory))
|
|
|
|
ARC_WEAK_WRITE(dst, 0);
|
|
|
|
else if (memoryType(PF->options, NSPointerFunctionsZeroingWeakMemory))
|
|
|
|
WEAK_WRITE(dst, (void*)0);
|
2009-02-15 07:13:54 +00:00
|
|
|
else
|
|
|
|
*dst = src;
|
|
|
|
}
|
|
|
|
}
|