Author: rfm
Date: Fri Jan  9 16:14:53 2015
New Revision: 38286

URL: http://svn.gna.org/viewcvs/gnustep?rev=38286&view=rev
Log:
add code to treat a FIFO as a container

Modified:
    libs/performance/trunk/ChangeLog
    libs/performance/trunk/GSFIFO.h
    libs/performance/trunk/GSFIFO.m

Modified: libs/performance/trunk/ChangeLog
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/performance/trunk/ChangeLog?rev=38286&r1=38285&r2=38286&view=diff
==============================================================================
--- libs/performance/trunk/ChangeLog    (original)
+++ libs/performance/trunk/ChangeLog    Fri Jan  9 16:14:53 2015
@@ -1,3 +1,8 @@
+2015-01-09 Richard Frith-Macdonald  <[email protected]>
+
+       * GSFIFO.[hm]: add methods for working with objects and handling
+       retain/release to treat a FIFO as a container.
+
 2014-11-12 Riccardo Mottola <[email protected]>
 
        * GSThreadPool.m

Modified: libs/performance/trunk/GSFIFO.h
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/performance/trunk/GSFIFO.h?rev=38286&r1=38285&r2=38286&view=diff
==============================================================================
--- libs/performance/trunk/GSFIFO.h     (original)
+++ libs/performance/trunk/GSFIFO.h     Fri Jan  9 16:14:53 2015
@@ -60,7 +60,11 @@
  * functions is not allowed (you must call the -get and -put: methods.<br />
  * It is recommended that you create FIFOs using the -initWithName: method
  * so that you can easily use the NSUserDefaults system to adjust their
- * configurations to tests/tweak performance.
+ * configurations to tests/tweak performance.<br />
+ * While a FIFO fundamentally works on abstract items without memory
+ * management, the API provides methods for handling NSObject values
+ * threating the FIFO as a container which retains the objects until
+ * they are removed from the FIFO.
  */
 @interface     GSFIFO : NSObject
 {
@@ -114,12 +118,32 @@
  */
 - (unsigned) get: (void**)buf  count: (unsigned)count  shouldBlock: 
(BOOL)block;
 
+/** Reads up to count objects from the FIFO (which must contain objects
+ * or nil items) into buf and autoreleases them.<br />
+ * If block is YES, this blocks if necessary until at least one object
+ * is available, and raises an exception if the FIFO is configured
+ * with a timeout and it is exceeded.<br />
+ * Returns the number of object actually read.
+ */
+- (unsigned) getObjects: (NSObject**)buf
+                  count: (unsigned)count
+            shouldBlock: (BOOL)block;
+
 /** 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 />
  * Implemented using -get:count:shouldBlock:
  */
 - (void*) get;
+
+/** Gets the next object from the FIFO (which must contain objects or
+ * nil items), blocking if necessary until an object is available.
+ * Autoreleases the object before returning it.<br />
+ * Raises an exception if the FIFO is configured
+ * with a timeout and it is exceeded.<br />
+ * Implemented using -get:count:shouldBlock:
+ */
+- (NSObject*) getObject;
 
 /** <init/>
  * Initialises the receiver with the specified capacity (buffer size).<br />
@@ -180,6 +204,16 @@
  */
 - (unsigned) put: (void**)buf  count: (unsigned)count  shouldBlock: 
(BOOL)block;
 
+/** Writes up to count objects from buf into the FIFO, retaining each.<br />
+ * If block is YES, this blocks if necessary until at least one object
+ * can be written, and raises an exception if the FIFO is configured
+ * with a timeout and it is exceeded.<br />
+ * Returns the number of objects actually written.
+ */
+- (unsigned) putObjects: (NSObject**)buf
+                  count: (unsigned)count
+            shouldBlock: (BOOL)block;
+
 /** 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 />
@@ -187,6 +221,14 @@
  */
 - (void) put: (void*)item;
 
+/** Adds an object to the FIFO (retaining the object), blocking if
+ * necessary until there is space in the buffer.<br />
+ * Raises an exception if the FIFO is configured
+ * with a timeout and it is exceeded.br />
+ * Implemented using -put:count:shouldBlock:
+ */
+- (void) putObject: (NSObject*)item;
+
 /** Return any available statistics for the receiver.<br />
  */
 - (NSString*) stats;
@@ -209,11 +251,23 @@
  */
 - (void*) tryGet;
 
-/** Attempts to put an item into the FIFO, returning YES on success or NO
- * if the FIFO is full.<br />
+/** Checks the FIFO and returns the first available object (autoreleased)
+ * or nil if the FIFO is empty (or contains a nil object).<br />
+ * Implemented using -get:count:shouldBlock:
+ */
+- (NSObject*) tryGetObject;
+
+/** Attempts to retain an object while putting it into the FIFO,
+ * returning YES on success or NO if the FIFO is full.<br />
  * Implemented using -put:count:shouldBlock:
  */
 - (BOOL) tryPut: (void*)item;
+
+/** Attempts to put an object (or nil) into the FIFO, returning YES
+ * on success or NO if the FIFO is full.<br />
+ * Implemented using -put:count:shouldBlock:
+ */
+- (BOOL) tryPutObject: (NSObject*)item;
 @end
 
 /** Function to efficiently get an item from a fast FIFO.<br />

Modified: libs/performance/trunk/GSFIFO.m
URL: 
http://svn.gna.org/viewcvs/gnustep/libs/performance/trunk/GSFIFO.m?rev=38286&r1=38285&r2=38286&view=diff
==============================================================================
--- libs/performance/trunk/GSFIFO.m     (original)
+++ libs/performance/trunk/GSFIFO.m     Fri Jan  9 16:14:53 2015
@@ -396,6 +396,21 @@
   return index;
 }
 
+- (unsigned) getObjects: (NSObject**)buf
+                  count: (unsigned)count
+            shouldBlock: (BOOL)block
+{
+  unsigned      result;
+  unsigned      index;
+
+  index = result = [self get: (void**)buf count: count shouldBlock: block];
+  while (index-- > 0)
+    {
+      [buf[index] release];
+    }
+  return result;
+}
+
 - (void*) get
 {
   void *item = 0;
@@ -403,6 +418,15 @@
   while (0 == [self get: &item count: 1 shouldBlock: YES])
     ;
   return item;
+}
+
+- (NSObject*) getObject
+{
+  void *item = 0;
+
+  while (0 == [self get: &item count: 1 shouldBlock: YES])
+    ;
+  return [(NSObject*)item autorelease];
 }
 
 - (id) initWithCapacity: (uint32_t)c
@@ -598,9 +622,41 @@
   return index;
 }
 
+- (unsigned) putObjects: (NSObject**)buf
+                  count: (unsigned)count
+            shouldBlock: (BOOL)block
+{
+  unsigned      result;
+  unsigned      index;
+
+  /* NB we must retain objects *before* putting them in the FIFO since
+   * another thread may grab them immediately.
+   * That means, if we fail to put all of the objects, we must release
+   * any that were left over.
+   */
+  for (index = 0; index < count; index++)
+    {
+      [buf[index] retain];
+    }
+  result = [self put: (void**)buf count: count shouldBlock: block];
+  while (count-- > result)
+    {
+      [buf[count] release];
+    }
+  return result;
+}
+
+
 - (void) put: (void*)item
 {
   while (0 == [self put: &item count: 1 shouldBlock: YES])
+    ;
+}
+
+- (void) putObject: (NSObject*)item
+{
+  [item retain];
+  while (0 == [self put: (void**)&item count: 1 shouldBlock: YES])
     ;
 }
 
@@ -749,6 +805,14 @@
   return item;
 }
 
+- (NSObject*) tryGetObject
+{
+  void *item = nil;
+  
+  [self get: &item count: 1 shouldBlock: NO];
+  return [(NSObject*)item autorelease];
+}
+
 - (BOOL) tryPut: (void*)item
 {
   if (1 == [self put: &item count: 1 shouldBlock: NO])
@@ -758,5 +822,16 @@
   return NO;
 }
 
+- (BOOL) tryPutObject: (NSObject*)item
+{
+  [item retain];
+  if (1 == [self put: (void**)&item count: 1 shouldBlock: NO])
+    {
+      return YES;
+    }
+  [item release];
+  return NO;
+}
+
 @end
 


_______________________________________________
Gnustep-cvs mailing list
[email protected]
https://mail.gna.org/listinfo/gnustep-cvs

Reply via email to