Files
lk/lib/evlog/evlog.c
Travis Geiselbrecht d8fa82cb91 [formatting] run everything through codestyle
Almost nothing changes here except moving braces to the same line as the
function declaration. Everything else is largely whitespace changes and
a few dangling files with tab indents.

See scripts/codestyle
2019-06-19 21:02:24 -07:00

78 lines
2.3 KiB
C

/*
* Copyright (c) 2012 Travis Geiselbrecht
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <lk/debug.h>
#include <assert.h>
#include <lk/err.h>
#include <lk/pow2.h>
#include <stdlib.h>
#include <lib/evlog.h>
#define INCPTR(e, ptr, inc) \
modpow2((ptr) + (inc), (e)->len_pow2)
status_t evlog_init_etc(evlog_t *e, uint len, uint unitsize, uintptr_t *items) {
if (len < 2 || !ispow2(len)) {
return ERR_INVALID_ARGS;
}
if (unitsize < 1 || !ispow2(unitsize)) {
return ERR_INVALID_ARGS;
}
if (unitsize > len) {
return ERR_INVALID_ARGS;
}
e->head = 0;
e->unitsize = unitsize;
e->len_pow2 = log2_uint(len);
e->items = items;
return NO_ERROR;
}
status_t evlog_init(evlog_t *e, uint len, uint unitsize) {
uintptr_t *items = calloc(1, len * sizeof(uintptr_t));
if (!items) {
return ERR_NO_MEMORY;
}
status_t err = evlog_init_etc(e, len, unitsize, items);
if (err < 0)
free(items);
return err;
}
uint evlog_bump_head(evlog_t *e) {
uint index = e->head;
e->head = INCPTR(e, e->head, e->unitsize);
return index;
}
void evlog_dump(evlog_t *e, evlog_dump_cb cb) {
for (uint index = INCPTR(e, e->head, e->unitsize); index != e->head; index = INCPTR(e, index, e->unitsize)) {
cb(&e->items[index]);
}
}