Skip to content

Commit

Permalink
Merge pull request #5 from benpicco/reed_solomon_init
Browse files Browse the repository at this point in the history
rs: repurpose reed_solomon_init() to initialize static buffer
  • Loading branch information
sleepybishop authored Mar 10, 2023
2 parents 395e5ad + 60dd03b commit e9e242e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
28 changes: 24 additions & 4 deletions rs.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,18 @@ void reed_solomon_init(void)
{
}

reed_solomon *reed_solomon_new(int ds, int ps)
reed_solomon *reed_solomon_new_static(void *buf, size_t len, int ds, int ps)
{
reed_solomon *rs = NULL;
reed_solomon *rs = buf;

if ((ds + ps) > DATA_SHARDS_MAX || ds <= 0 || ps <= 0)
return NULL;
rs = calloc(1, sizeof(reed_solomon) + 2 * ps * ds);
if (!rs)

if (len < reed_solomon_bufsize(ds, ps))
return NULL;

memset(buf, 0, len);

rs->ds = ds;
rs->ps = ps;
rs->ts = ds + ps;
Expand All @@ -96,9 +100,25 @@ reed_solomon *reed_solomon_new(int ds, int ps)
for (int i = 0; i < rs->ds; i++)
row[i] = GF2_8_INV[(rs->ps + i) ^ j];
}

return rs;
}

reed_solomon *reed_solomon_new(int ds, int ps)
{
size_t len = reed_solomon_bufsize(ds, ps);
void *buf = malloc(len);
if (!buf)
return NULL;

if (reed_solomon_new_static(buf, len, ds, ps) == NULL) {
free(buf);
return NULL;
}

return buf;
}

void reed_solomon_release(reed_solomon *rs)
{
if (rs)
Expand Down
3 changes: 2 additions & 1 deletion rs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ typedef struct _reed_solomon {
uint8_t p[];
} reed_solomon;

//#define reed_solomon_init()
#define reed_solomon_bufsize(ds, ps) (sizeof(reed_solomon) + 2 * (ps) * (ds))
#define reed_solomon_reconstruct reed_solomon_decode

void reed_solomon_init(void);
reed_solomon *reed_solomon_new_static(void *buf, size_t len, int ds, int ps);
reed_solomon *reed_solomon_new(int data_shards, int parity_shards);
void reed_solomon_release(reed_solomon *rs);

Expand Down

0 comments on commit e9e242e

Please sign in to comment.