Skip to content

Commit 937dfef

Browse files
authored
Merge pull request #3 from RinHizakura/pr_ringbuffer
ringbuffer: Prevent inappropriate allocation size
2 parents 43b2aa9 + c66ec56 commit 937dfef

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

ringbuffer/ringbuffer.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ typedef struct {
5454
/* true if x is a power of 2 */
5555
#define IS_POWEROF2(x) ((((x) -1) & (x)) == 0)
5656
#define RING_SIZE_MASK (unsigned) (0x0fffffff) /**< Ring size mask */
57-
#define ALIGN_FLOOR(val, align) \
58-
(typeof(val))((val) & (~((typeof(val))((align) -1))))
57+
#define ALIGN_CEIL(val, align) \
58+
(typeof(val))((val) + (-(typeof(val))(val) & ((align) -1)))
59+
5960

6061
/**
6162
* Calculate the memory size needed for a ring buffer.
@@ -80,7 +81,7 @@ ssize_t ringbuf_get_memsize(const unsigned count)
8081
return -EINVAL;
8182

8283
ssize_t sz = sizeof(ringbuf_t) + count * sizeof(void *);
83-
sz = ALIGN_FLOOR(sz, CACHE_LINE_SIZE);
84+
sz = ALIGN_CEIL(sz, CACHE_LINE_SIZE);
8485
return sz;
8586
}
8687

0 commit comments

Comments
 (0)