Add -getObjectRetained and -putObjectConsumed: methods

This commit is contained in:
Richard Frith-Macdonald 2021-05-22 11:09:12 +01:00
parent 5cf55f34c2
commit 3b16cbb283
2 changed files with 38 additions and 2 deletions

View file

@ -142,7 +142,8 @@
/**
* Reads up to count autoreleased objects from the FIFO into the buf. If blocking
* Reads up to count autoreleased objects from the FIFO into the buf.
* If blocking
* is requested and a before date is specified, the operation blocks until the
* specified time and returns 0 if it could not read any items. The timeout
* configured for the FIFO still takes precedence.
@ -168,6 +169,14 @@
*/
- (NSObject*) getObject;
/** Gets the next item from the FIFO, blocking if necessary until an
* item is available. Raises an exception if the FIFO is configured
* with a timeout and it is exceeded.<br />
* The returned item/object, is not autoreleased.<br />
* Implemented using -get:count:shouldBlock:
*/
- (NSObject*) getObjectRetained NS_RETURNS_RETAINED;
/** <init/>
* Initialises the receiver with the specified capacity (buffer size).<br />
* The capacity must lie in the range from one to a hundred million, otherwise
@ -252,6 +261,9 @@
/** Adds an item to the FIFO, blocking if necessary until there is
* space in the buffer. Raises an exception if the FIFO is configured
* with a timeout and it is exceeded.br />
* The item may be an object (which is not retained when added) or a
* pointer to memory. The ownership of the item memory should be
* transferred to the FIFO.<br />
* Implemented using -put:count:shouldBlock:
*/
- (void) put: (void*)item;
@ -264,6 +276,15 @@
*/
- (void) putObject: (NSObject*)item;
/** Adds an object to the FIFO, blocking if necessary until there is
* space in the buffer. Raises an exception if the FIFO is configured
* with a timeout and it is exceeded.br />
* The object is not retained when added, so ownership of the memory
* should be transferred to the FIFO.<br />
* Implemented using -put:count:shouldBlock:
*/
- (void) putObjectConsumed: (NSObject*) NS_CONSUMED item;
/** Return any available statistics for the receiver.<br />
*/
- (NSString*) stats;

View file

@ -629,6 +629,15 @@ stats(NSTimeInterval ti, uint32_t max, NSTimeInterval *bounds, uint64_t *bands)
return [(NSObject*)item autorelease];
}
- (NSObject*) getObjectRetained NS_RETURNS_RETAINED
{
void *item = 0;
while (0 == [self get: &item count: 1 shouldBlock: YES])
;
return (NSObject*)item;
}
- (id) initWithCapacity: (uint32_t)c
granularity: (uint16_t)g
timeout: (uint16_t)t
@ -858,7 +867,7 @@ stats(NSTimeInterval ti, uint32_t max, NSTimeInterval *bounds, uint64_t *bands)
- (void) put: (void*)item
{
while (0 == [self put: &item count: 1 shouldBlock: YES])
while (0 == [self put: (void**)&item count: 1 shouldBlock: YES])
;
}
@ -869,6 +878,12 @@ stats(NSTimeInterval ti, uint32_t max, NSTimeInterval *bounds, uint64_t *bands)
;
}
- (void) putObjectConsumed: (NSObject*) NS_CONSUMED item
{
while (0 == [self put: (void**)&item count: 1 shouldBlock: YES])
;
}
- (void) _getStats: (NSMutableString*)s
{
[s appendFormat: