Add functions to help with link store

This commit is contained in:
Richard Frith-Macdonald 2022-08-10 21:36:48 +01:00
parent 25e60c6301
commit 09dfc1c403
2 changed files with 27 additions and 22 deletions

View file

@ -387,6 +387,30 @@ GSLinkedListRemove(GSListLink *link, GSLinkedList *list);
@end @end
static inline void
GSLinkStoreConsumeLink(GSLinkStore *list, GSListLink NS_CONSUMED *link)
{
link->next = list->free;
list->free = link;
}
static inline GSListLink* NS_RETURNS_RETAINED
GSLinkStoreProvideLink(GSLinkStore *list)
{
GSListLink *link = list->free;
if (nil == link)
{
link = [list->linkClass new];
}
else
{
list->free = link->next;
link->next = nil;
}
return link;
}
/** Adds the object to the list after the specified link.<br /> /** Adds the object to the list after the specified link.<br />
* Calls GSLinkedListInsertAfter().<br /> * Calls GSLinkedListInsertAfter().<br />
* Returns the list link that the object is stored in. * Returns the list link that the object is stored in.

View file

@ -618,17 +618,8 @@ GSListLink*
GSLinkStoreInsertObjectAfter( GSLinkStoreInsertObjectAfter(
NSObject *anObject, GSLinkStore *list, GSListLink *at) NSObject *anObject, GSLinkStore *list, GSListLink *at)
{ {
GSListLink *link = list->free; GSListLink *link = GSLinkStoreProvideLink(list);
if (nil == link)
{
link = [list->linkClass new];
}
else
{
list->free = link->next;
link->next = nil;
}
link->item = RETAIN(anObject); link->item = RETAIN(anObject);
GSLinkedListInsertAfter(link, list, (nil == at) ? list->tail : at); GSLinkedListInsertAfter(link, list, (nil == at) ? list->tail : at);
return link; return link;
@ -638,17 +629,8 @@ GSListLink*
GSLinkStoreInsertObjectBefore( GSLinkStoreInsertObjectBefore(
NSObject *anObject, GSLinkStore *list, GSListLink *at) NSObject *anObject, GSLinkStore *list, GSListLink *at)
{ {
GSListLink *link = list->free; GSListLink *link = GSLinkStoreProvideLink(list);
if (nil == link)
{
link = [list->linkClass new];
}
else
{
list->free = link->next;
link->next = nil;
}
link->item = RETAIN(anObject); link->item = RETAIN(anObject);
GSLinkedListInsertBefore(link, list, (nil == at) ? list->head : at); GSLinkedListInsertBefore(link, list, (nil == at) ? list->head : at);
return link; return link;
@ -662,8 +644,7 @@ GSLinkStoreRemoveObjectAt(GSLinkStore *list, GSListLink *at)
GSLinkedListRemove(at, list); GSLinkedListRemove(at, list);
RELEASE(at->item); RELEASE(at->item);
at->item = nil; at->item = nil;
at->next = list->free; GSLinkStoreConsumeLink(list, at);
list->free = at;
} }
} }