]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: ring: ring_cast_from_area() cast from an allocated area
authorWilliam Lallemand <wlallemand@haproxy.org>
Tue, 27 Sep 2022 13:53:53 +0000 (15:53 +0200)
committerWilliam Lallemand <wlallemand@haproxy.org>
Thu, 13 Oct 2022 14:45:28 +0000 (16:45 +0200)
Cast an unified ring + storage area to a ring from area, without
reinitializing the data buffer. Reinitialize the waiters and the lock.

It helps retrieving a previously allocated ring, from an mmap for
example.

include/haproxy/ring.h
src/ring.c

index bab18c679177d21373b166be548b139b35f48b37..d6c527c5bc78594d3dfd5aa8642a9e3e35c4777a 100644 (file)
@@ -30,6 +30,7 @@ struct appctx;
 
 struct ring *ring_new(size_t size);
 struct ring *ring_make_from_area(void *area, size_t size);
+struct ring *ring_cast_from_area(void *area);
 void ring_init(struct ring *ring, void* area, size_t size);
 struct ring *ring_resize(struct ring *ring, size_t size);
 void ring_free(struct ring *ring);
index da987c5d5fd44b0fcc5976d026562eebd5a8c258..91157a90b39108b29be8181242b8caacffe8590d 100644 (file)
@@ -99,6 +99,25 @@ struct ring *ring_make_from_area(void *area, size_t size)
        return ring;
 }
 
+/* Cast an unified ring + storage area to a ring from <area>, without
+ * reinitializing the data buffer.
+ *
+ * Reinitialize the waiters and the lock.
+ */
+struct ring *ring_cast_from_area(void *area)
+{
+       struct ring *ring = NULL;
+
+       ring = area;
+       ring->buf.area = area + sizeof(*ring);
+
+       HA_RWLOCK_INIT(&ring->lock);
+       LIST_INIT(&ring->waiters);
+       ring->readers_count = 0;
+
+       return ring;
+}
+
 /* Resizes existing ring <ring> to <size> which must be larger, without losing
  * its contents. The new size must be at least as large as the previous one or
  * no change will be performed. The pointer to the ring is returned on success,