Skip to content

Commit

Permalink
lone: define memory comparison functions
Browse files Browse the repository at this point in the history
I will probably need them soon.
  • Loading branch information
matheusmoreira committed Nov 16, 2023
1 parent 8c023fd commit 20500f2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/lone/memory/functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <lone/types.h>

int lone_memory_compare(unsigned char *a, unsigned char *b, size_t count);
bool lone_memory_is_equal(unsigned char *a, unsigned char *b, size_t count);
void lone_memory_move(void *from, void *to, size_t count);
void lone_memory_set(void *to, unsigned char byte, size_t count);
void lone_memory_zero(void *to, size_t count);
Expand Down
28 changes: 28 additions & 0 deletions source/lone/memory/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@

#include <lone/memory/functions.h>

int lone_memory_compare(unsigned char *a, unsigned char *b, size_t count)
{
size_t i;
unsigned char x, y;

if (a == b || count == 0) {
return 0;
}

for (i = 0; i < count; ++i) {
x = a[i];
y = b[i];

if (x == y) {
continue;
}

return x - y;
}

return 0;
}

bool lone_memory_is_equal(unsigned char *a, unsigned char *b, size_t count)
{
return lone_memory_compare(a, b, count) == 0;
}

void lone_memory_move(void *from, void *to, size_t count)
{
unsigned char *source = from, *destination = to;
Expand Down

0 comments on commit 20500f2

Please sign in to comment.