Skip to content

Commit

Permalink
Move buffer_find to assembler.c, it isn't actually common
Browse files Browse the repository at this point in the history
  • Loading branch information
flibitijibibo committed Apr 1, 2020
1 parent 2b018ba commit aafeedb
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 115 deletions.
96 changes: 96 additions & 0 deletions mojoshader_assembler.c
Expand Up @@ -1492,6 +1492,102 @@ static const MOJOSHADER_parseData *build_failed_assembly(Context *ctx)
} // build_failed_assembly


static int blockscmp(BufferBlock *item, const uint8 *data, size_t len)
{
if (len == 0)
return 1; // "match"

while (item != NULL)
{
const size_t itemremain = item->bytes;
const size_t avail = len < itemremain ? len : itemremain;
if (memcmp(item->data, data, avail) != 0)
return 0; // not a match.

if (len == avail)
return 1; // complete match!

len -= avail;
data += avail;
item = item->next;
} // while

return 0; // not a complete match.
} // blockscmp


ssize_t buffer_find(Buffer *buffer, const size_t start,
const void *_data, const size_t len)
{
if (len == 0)
return 0; // I guess that's right.

if (start >= buffer->total_bytes)
return -1; // definitely can't match.

if (len > (buffer->total_bytes - start))
return -1; // definitely can't match.

// Find the start point somewhere in the center of a buffer.
BufferBlock *item = buffer->head;
const uint8 *ptr = item->data;
size_t pos = 0;
if (start > 0)
{
while (1)
{
assert(item != NULL);
if ((pos + item->bytes) > start) // start is in this block.
{
ptr = item->data + (start - pos);
break;
} // if

pos += item->bytes;
item = item->next;
} // while
} // if

// okay, we're at the origin of the search.
assert(item != NULL);
assert(ptr != NULL);

const uint8 *data = (const uint8 *) _data;
const uint8 first = *data;
while (item != NULL)
{
const size_t itemremain = item->bytes - ((size_t)(ptr-item->data));
ptr = (uint8 *) memchr(ptr, first, itemremain);
while (ptr != NULL)
{
const size_t retval = pos + ((size_t) (ptr - item->data));
if (len == 1)
return retval; // we're done, here it is!

const size_t itemremain = item->bytes - ((size_t)(ptr-item->data));
const size_t avail = len < itemremain ? len : itemremain;
if ((avail == 0) || (memcmp(ptr, data, avail) == 0))
{
// okay, we've got a (sub)string match! Move to the next block.
// check all blocks until we get a complete match or a failure.
if (blockscmp(item->next, data+avail, len-avail))
return (ssize_t) retval;
} // if

// try again, further in this block.
ptr = (uint8 *) memchr(ptr + 1, first, itemremain - 1);
} // while

pos += item->bytes;
item = item->next;
if (item != NULL)
ptr = item->data;
} // while

return -1; // no match found.
} // buffer_find


static uint32 add_ctab_bytes(Context *ctx, const uint8 *bytes, const size_t len)
{
if (isfail(ctx))
Expand Down
112 changes: 0 additions & 112 deletions mojoshader_common.c
Expand Up @@ -674,24 +674,6 @@ void errorlist_destroy(ErrorList *list)
} // errorlist_destroy


typedef struct BufferBlock
{
uint8 *data;
size_t bytes;
struct BufferBlock *next;
} BufferBlock;

struct Buffer
{
size_t total_bytes;
BufferBlock *head;
BufferBlock *tail;
size_t block_size;
MOJOSHADER_malloc m;
MOJOSHADER_free f;
void *d;
};

Buffer *buffer_create(size_t blksz, MOJOSHADER_malloc m,
MOJOSHADER_free f, void *d)
{
Expand Down Expand Up @@ -942,100 +924,6 @@ void buffer_destroy(Buffer *buffer)
} // if
} // buffer_destroy

static int blockscmp(BufferBlock *item, const uint8 *data, size_t len)
{
if (len == 0)
return 1; // "match"

while (item != NULL)
{
const size_t itemremain = item->bytes;
const size_t avail = len < itemremain ? len : itemremain;
if (memcmp(item->data, data, avail) != 0)
return 0; // not a match.

if (len == avail)
return 1; // complete match!

len -= avail;
data += avail;
item = item->next;
} // while

return 0; // not a complete match.
} // blockscmp

ssize_t buffer_find(Buffer *buffer, const size_t start,
const void *_data, const size_t len)
{
if (len == 0)
return 0; // I guess that's right.

if (start >= buffer->total_bytes)
return -1; // definitely can't match.

if (len > (buffer->total_bytes - start))
return -1; // definitely can't match.

// Find the start point somewhere in the center of a buffer.
BufferBlock *item = buffer->head;
const uint8 *ptr = item->data;
size_t pos = 0;
if (start > 0)
{
while (1)
{
assert(item != NULL);
if ((pos + item->bytes) > start) // start is in this block.
{
ptr = item->data + (start - pos);
break;
} // if

pos += item->bytes;
item = item->next;
} // while
} // if

// okay, we're at the origin of the search.
assert(item != NULL);
assert(ptr != NULL);

const uint8 *data = (const uint8 *) _data;
const uint8 first = *data;
while (item != NULL)
{
const size_t itemremain = item->bytes - ((size_t)(ptr-item->data));
ptr = (uint8 *) memchr(ptr, first, itemremain);
while (ptr != NULL)
{
const size_t retval = pos + ((size_t) (ptr - item->data));
if (len == 1)
return retval; // we're done, here it is!

const size_t itemremain = item->bytes - ((size_t)(ptr-item->data));
const size_t avail = len < itemremain ? len : itemremain;
if ((avail == 0) || (memcmp(ptr, data, avail) == 0))
{
// okay, we've got a (sub)string match! Move to the next block.
// check all blocks until we get a complete match or a failure.
if (blockscmp(item->next, data+avail, len-avail))
return (ssize_t) retval;
} // if

// try again, further in this block.
ptr = (uint8 *) memchr(ptr + 1, first, itemremain - 1);
} // while

pos += item->bytes;
item = item->next;
if (item != NULL)
ptr = item->data;
} // while

return -1; // no match found.
} // buffer_find

void buffer_patch(Buffer *buffer, const size_t start,
const void *_data, const size_t len)
{
Expand Down
19 changes: 16 additions & 3 deletions mojoshader_internal.h
Expand Up @@ -264,7 +264,22 @@ void errorlist_destroy(ErrorList *list);

// Dynamic buffers...

typedef struct Buffer Buffer;
typedef struct BufferBlock
{
uint8 *data;
size_t bytes;
struct BufferBlock *next;
} BufferBlock;
typedef struct Buffer
{
size_t total_bytes;
BufferBlock *head;
BufferBlock *tail;
size_t block_size;
MOJOSHADER_malloc m;
MOJOSHADER_free f;
void *d;
} Buffer;
Buffer *buffer_create(size_t blksz,MOJOSHADER_malloc m,MOJOSHADER_free f,void *d);
char *buffer_reserve(Buffer *buffer, const size_t len);
int buffer_append(Buffer *buffer, const void *_data, size_t len);
Expand All @@ -275,8 +290,6 @@ void buffer_empty(Buffer *buffer);
char *buffer_flatten(Buffer *buffer);
char *buffer_merge(Buffer **buffers, const size_t n, size_t *_len);
void buffer_destroy(Buffer *buffer);
ssize_t buffer_find(Buffer *buffer, const size_t start,
const void *data, const size_t len);
void buffer_patch(Buffer *buffer, const size_t start,
const void *data, const size_t len);

Expand Down

0 comments on commit aafeedb

Please sign in to comment.