'
This commit is contained in:
commit
d0bd16da2f
9 changed files with 97 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
a.out
|
||||||
|
*.o
|
||||||
|
*.a
|
||||||
11
beware_overflow/beware_overflow.c
Normal file
11
beware_overflow/beware_overflow.c
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#include "beware_overflow.h"
|
||||||
|
|
||||||
|
void *beware_overflow(void *ptr, size_t nmemb, size_t size)
|
||||||
|
{
|
||||||
|
size_t c;
|
||||||
|
if (__builtin_mul_overflow(nmemb, size, &c))
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
char *byte_ptr = ptr;
|
||||||
|
return c + byte_ptr;
|
||||||
|
}
|
||||||
8
beware_overflow/beware_overflow.h
Normal file
8
beware_overflow/beware_overflow.h
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef BEWARE_OVERFLOW_H
|
||||||
|
#define BEWARE_OVERFLOW_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
void *beware_overflow(void *ptr, size_t nmemb, size_t size);
|
||||||
|
|
||||||
|
#endif /* BEWARE_OVERFLOW_H */
|
||||||
21
beware_overflow/main.c
Normal file
21
beware_overflow/main.c
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "beware_overflow.h"
|
||||||
|
|
||||||
|
void print_overflow(void *ptr, size_t nmemb, size_t size)
|
||||||
|
{
|
||||||
|
void *begin = ptr;
|
||||||
|
void *res = beware_overflow(ptr, nmemb, size);
|
||||||
|
if (res)
|
||||||
|
printf("Pointer was incremented from %p to %p.\n", begin, res);
|
||||||
|
else
|
||||||
|
printf("Overflow detected between %ld and %ld.\n", nmemb, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
print_overflow((void *)0x1000, 25, 6);
|
||||||
|
print_overflow((void *)0x40000, 12345678904, 12345678904);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
1
my_recycler/my_recycler.c
Normal file
1
my_recycler/my_recycler.c
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
#include "my_recycler.h"
|
||||||
24
my_recycler/my_recycler.h
Normal file
24
my_recycler/my_recycler.h
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#ifndef MY_RECYCLER_H
|
||||||
|
#define MY_RECYCLER_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
struct recycler
|
||||||
|
{
|
||||||
|
size_t block_size; // size of a block
|
||||||
|
size_t capacity; // number of blocks in the chunk
|
||||||
|
void *chunk; // memory chunk containing all blocks
|
||||||
|
void *free; // address of the first free block of the free list
|
||||||
|
};
|
||||||
|
|
||||||
|
struct free_list
|
||||||
|
{
|
||||||
|
struct free_list *next; // next free block
|
||||||
|
};
|
||||||
|
|
||||||
|
struct recycler *recycler_create(size_t block_size, size_t total_size);
|
||||||
|
void recycler_destroy(struct recycler *r);
|
||||||
|
void *recycler_allocate(struct recycler *r);
|
||||||
|
void recycler_free(struct recycler *r, void *block);
|
||||||
|
|
||||||
|
#endif /* ! MY_RECYCLER_H */
|
||||||
21
page_begin/main.c
Normal file
21
page_begin/main.c
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "page_begin.h"
|
||||||
|
|
||||||
|
static void display_result(void *ptr, size_t page_size, void *expected_result)
|
||||||
|
{
|
||||||
|
void *res = page_begin(ptr, page_size);
|
||||||
|
|
||||||
|
printf("ptr: %p\n", ptr);
|
||||||
|
printf("page_size: %lu\n", page_size);
|
||||||
|
printf("expected_result: %p\n", expected_result);
|
||||||
|
printf("result: %p\n", res);
|
||||||
|
|
||||||
|
printf(expected_result == res ? "OK\n" : "KO\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
display_result((void *)0x1234ffea, 4096, (void *)0x1234f000);
|
||||||
|
display_result((void *)0x1234ffea, 256, (void *)0x1234ff00);
|
||||||
|
}
|
||||||
0
page_begin/page_begin.c
Normal file
0
page_begin/page_begin.c
Normal file
8
page_begin/page_begin.h
Normal file
8
page_begin/page_begin.h
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
#ifndef PAGE_BEGIN_H
|
||||||
|
#define PAGE_BEGIN_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
void *page_begin(void *ptr, size_t page_size);
|
||||||
|
|
||||||
|
#endif /* !PAGE_BEGIN_H */
|
||||||
Loading…
Add table
Add a link
Reference in a new issue