1 /* -*- Mode: C; c-basic-offset:4 ; -*- */
2 /*
3 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
4 * University Research and Technology
5 * Corporation. All rights reserved.
6 * Copyright (c) 2004-2008 The University of Tennessee and The University
7 * of Tennessee Research Foundation. All rights
8 * reserved.
9 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
10 * University of Stuttgart. All rights reserved.
11 * Copyright (c) 2004-2005 The Regents of the University of California.
12 * All rights reserved.
13 * Copyright (c) 2010 Cisco Systems, Inc. All rights reserved.
14 * Copyright (c) 2016 Intel, Inc. All rights reserved.
15 * $COPYRIGHT$
16 *
17 * Additional copyrights may follow
18 *
19 * $HEADER$
20 */
21 /** @file
22 *
23 */
24
25 #ifndef PMIX_RING_BUFFER_H
26 #define PMIX_RING_BUFFER_H
27
28 #include <src/include/pmix_config.h>
29
30 #include "src/class/pmix_object.h"
31 #include "src/util/output.h"
32
33 BEGIN_C_DECLS
34
35 /**
36 * dynamic pointer ring
37 */
38 struct pmix_ring_buffer_t {
39 /** base class */
40 pmix_object_t super;
41 /* head/tail indices */
42 int head;
43 int tail;
44 /** size of list, i.e. number of elements in addr */
45 int size;
46 /** pointer to ring */
47 char **addr;
48 };
49 /**
50 * Convenience typedef
51 */
52 typedef struct pmix_ring_buffer_t pmix_ring_buffer_t;
53 /**
54 * Class declaration
55 */
56 PMIX_CLASS_DECLARATION(pmix_ring_buffer_t);
57
58 /**
59 * Initialize the ring buffer, defining its size.
60 *
61 * @param ring Pointer to a ring buffer (IN/OUT)
62 * @param size The number of elements in the ring (IN)
63 *
64 * @return PMIX_SUCCESS if all initializations were succesful. Otherwise,
65 * the error indicate what went wrong in the function.
66 */
67 PMIX_EXPORT int pmix_ring_buffer_init(pmix_ring_buffer_t* ring, int size);
68
69 /**
70 * Push an item onto the ring buffer, displacing the oldest
71 * item on the ring if the ring is full
72 *
73 * @param ring Pointer to ring (IN)
74 * @param ptr Pointer value (IN)
75 *
76 * @return Pointer to displaced item, NULL if ring
77 * is not yet full
78 */
79 PMIX_EXPORT void* pmix_ring_buffer_push(pmix_ring_buffer_t *ring, void *ptr);
80
81
82 /**
83 * Pop an item off of the ring. The oldest entry on the ring will be
84 * returned. If nothing on the ring, NULL is returned.
85 *
86 * @param ring Pointer to ring (IN)
87 *
88 * @return Error code. NULL indicates an error.
89 */
90
91 PMIX_EXPORT void* pmix_ring_buffer_pop(pmix_ring_buffer_t *ring);
92
93 /*
94 * Access an element of the ring, without removing it, indexed
95 * starting at the tail - a value of -1 will return the element
96 * at the head of the ring
97 */
98 PMIX_EXPORT void* pmix_ring_buffer_poke(pmix_ring_buffer_t *ring, int i);
99
100 END_C_DECLS
101
102 #endif /* PMIX_RING_BUFFER_H */