Skip to content

Commit

Permalink
Use const pointers for associated object keys.
Browse files Browse the repository at this point in the history
Matches Apple platform implementations.
  • Loading branch information
triplef authored and davidchisnall committed Jul 10, 2019
1 parent d16faed commit 5308d21
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Test/AssociatedObject.m
Expand Up @@ -48,7 +48,7 @@ int main(void)
int lost = 0;
for (uintptr_t i = 1; i <= 20; ++i)
{
if (object != objc_getAssociatedObject(holder, (void*)i))
if (object != objc_getAssociatedObject(holder, (const void*)i))
{
fprintf(stderr, "lost object %" PRIuPTR "\n", i);
++lost;
Expand Down
10 changes: 5 additions & 5 deletions associate.m
Expand Up @@ -22,7 +22,7 @@
* The key used for identifying this object. Opaque pointer, should be set
* to 0 when this slot is unused.
*/
void *key;
const void *key;
/**
* The associated object. Note, if the policy is assign then this may be
* some other type of pointer...
Expand Down Expand Up @@ -73,7 +73,7 @@ static BOOL isAtomic(uintptr_t policy)
return (policy & OBJC_ASSOCIATION_ATOMIC) == OBJC_ASSOCIATION_ATOMIC;
}

static struct reference* findReference(struct reference_list *list, void *key)
static struct reference* findReference(struct reference_list *list, const void *key)
{
while (list)
{
Expand Down Expand Up @@ -121,7 +121,7 @@ static void freeReferenceList(struct reference_list *l)
}

static void setReference(struct reference_list *list,
void *key,
const void *key,
void *obj,
uintptr_t policy)
{
Expand Down Expand Up @@ -324,7 +324,7 @@ static void deallocHiddenClass(id obj, SEL _cmd)
}

void objc_setAssociatedObject(id object,
void *key,
const void *key,
id value,
objc_AssociationPolicy policy)
{
Expand All @@ -333,7 +333,7 @@ void objc_setAssociatedObject(id object,
setReference(list, key, value, policy);
}

id objc_getAssociatedObject(id object, void *key)
id objc_getAssociatedObject(id object, const void *key)
{
if (isSmallObject(object)) { return nil; }
struct reference_list *list = referenceListForObject(object, NO);
Expand Down
4 changes: 2 additions & 2 deletions objc/runtime.h
Expand Up @@ -1054,7 +1054,7 @@ typedef uintptr_t objc_AssociationPolicy;
* with the same arguments, or nil if none exists.
*/
OBJC_PUBLIC
id objc_getAssociatedObject(id object, void *key);
id objc_getAssociatedObject(id object, const void *key);
/**
* Associates an object with another. This provides a mechanism for storing
* extra state with an object, beyond its declared instance variables. The
Expand All @@ -1064,7 +1064,7 @@ id objc_getAssociatedObject(id object, void *key);
* if an association policy of copy or retain is passed as the final argument.
*/
OBJC_PUBLIC
void objc_setAssociatedObject(id object, void *key, id value, objc_AssociationPolicy policy);
void objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy);
/**
* Removes all associations from an object.
*/
Expand Down

0 comments on commit 5308d21

Please sign in to comment.