Rename: read_ptr -> idx
This commit is contained in:
parent
e07d355da4
commit
12590533a7
1 changed files with 16 additions and 16 deletions
32
stack.c
32
stack.c
|
|
@ -16,10 +16,10 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t *buf; //!< Pointer to the internal buffer
|
uint8_t *buf; //!< Pointer to the internal buffer
|
||||||
size_t buf_sz; //!< The length of the allocated buffer, in bytes
|
size_t buf_sz; //!< The length of the allocated buffer, in bytes
|
||||||
size_t read_ptr; //!< Indicates where to read/write
|
size_t idx; //!< Indicates where to read/write
|
||||||
size_t item_sz; //!< The size of the contained item, in bytes
|
size_t item_sz; //!< The size of the contained item, in bytes
|
||||||
} Stack;
|
} Stack;
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
|
@ -38,7 +38,7 @@ MUST_CHECK StackResult stack_push(Stack *stack, const void *value);
|
||||||
MUST_CHECK StackResult stack_pop(Stack *stack, void *dest);
|
MUST_CHECK StackResult stack_pop(Stack *stack, void *dest);
|
||||||
|
|
||||||
static inline size_t stack_length(Stack *stack) {
|
static inline size_t stack_length(Stack *stack) {
|
||||||
return (stack->read_ptr / stack->item_sz);
|
return (stack->idx / stack->item_sz);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline size_t stack_capacity(Stack *stack) {
|
static inline size_t stack_capacity(Stack *stack) {
|
||||||
|
|
@ -46,11 +46,11 @@ static inline size_t stack_capacity(Stack *stack) {
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool stack_full(Stack *stack) {
|
static inline bool stack_full(Stack *stack) {
|
||||||
return (stack->buf_sz == stack->read_ptr);
|
return (stack->buf_sz == stack->idx);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline bool stack_empty(Stack *stack) {
|
static inline bool stack_empty(Stack *stack) {
|
||||||
return (stack->read_ptr < stack->item_sz);
|
return (stack->idx < stack->item_sz);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // STACK_H
|
#endif // STACK_H
|
||||||
|
|
@ -73,7 +73,7 @@ StackResult stack_init_raw(Stack *stack, uint8_t *buf, size_t buf_sz, size_t ite
|
||||||
|
|
||||||
stack->buf_sz = buf_sz;
|
stack->buf_sz = buf_sz;
|
||||||
stack->item_sz = item_sz;
|
stack->item_sz = item_sz;
|
||||||
stack->read_ptr = 0;
|
stack->idx = 0;
|
||||||
|
|
||||||
memset(stack->buf, 0, buf_sz);
|
memset(stack->buf, 0, buf_sz);
|
||||||
|
|
||||||
|
|
@ -90,7 +90,7 @@ StackResult stack_resize(Stack *stack, size_t capacity) {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *stack_resize_raw(Stack *stack, uint8_t *new_buf, size_t buf_sz) {
|
uint8_t *stack_resize_raw(Stack *stack, uint8_t *new_buf, size_t buf_sz) {
|
||||||
if (stack->read_ptr > buf_sz)
|
if (stack->idx > buf_sz)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
uint8_t *old_buf = stack->buf;
|
uint8_t *old_buf = stack->buf;
|
||||||
|
|
@ -103,21 +103,21 @@ uint8_t *stack_resize_raw(Stack *stack, uint8_t *new_buf, size_t buf_sz) {
|
||||||
}
|
}
|
||||||
|
|
||||||
StackResult stack_push(Stack *stack, const void *value) {
|
StackResult stack_push(Stack *stack, const void *value) {
|
||||||
if (stack->read_ptr + stack->item_sz > stack->buf_sz)
|
if (stack->idx + stack->item_sz > stack->buf_sz)
|
||||||
return StackFull;
|
return StackFull;
|
||||||
|
|
||||||
memcpy((char *)stack->buf + stack->read_ptr, value, stack->item_sz);
|
memcpy((char *)stack->buf + stack->idx, value, stack->item_sz);
|
||||||
stack->read_ptr += stack->item_sz;
|
stack->idx += stack->item_sz;
|
||||||
|
|
||||||
return Ok;
|
return Ok;
|
||||||
}
|
}
|
||||||
|
|
||||||
StackResult stack_pop(Stack *stack, void *dest) {
|
StackResult stack_pop(Stack *stack, void *dest) {
|
||||||
if (stack->read_ptr < stack->item_sz)
|
if (stack->idx < stack->item_sz)
|
||||||
return StackEmpty;
|
return StackEmpty;
|
||||||
|
|
||||||
stack->read_ptr -= stack->item_sz;
|
stack->idx -= stack->item_sz;
|
||||||
memcpy(dest, (char *)stack->buf + stack->read_ptr, stack->item_sz);
|
memcpy(dest, (char *)stack->buf + stack->idx, stack->item_sz);
|
||||||
|
|
||||||
return Ok;
|
return Ok;
|
||||||
}
|
}
|
||||||
|
|
@ -128,7 +128,7 @@ StackResult stack_destroy(Stack *stack) {
|
||||||
stack->buf = NULL;
|
stack->buf = NULL;
|
||||||
stack->buf_sz = 0;
|
stack->buf_sz = 0;
|
||||||
stack->item_sz = 0;
|
stack->item_sz = 0;
|
||||||
stack->read_ptr = 0;
|
stack->idx = 0;
|
||||||
|
|
||||||
return Ok;
|
return Ok;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue