Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- int *p;
- void *memory_alloc(unsigned int size)
- {
- int x=1, zvysok;
- for(;;){
- if (p[0] >= size + 4){
- if(size % 4 != 0) {
- if (p[x] >= 0) {
- if (p[x]> size){
- p[x] = p[x] * (-1);
- p[0] = p[0] - ((p[x]*(-1)) + 4);
- return &p[x+1];
- }
- else if ((size > p[x]) && (p[x] != 0))
- return NULL;
- else {
- zvysok = size % 4;
- p[x] = ((size + (4-zvysok)) * (-1));
- p[0] = p[0] - (size + (4-zvysok) + 4);
- return &p[x+1];
- }
- }
- else {
- if (p[x] < 0){
- x = x +1+ ((p[x]*(-1))/4);
- }
- else if (p[x] >= 0){
- x = x +1+ ((p[x])/4);
- }
- }
- }
- else{
- if (p[x] >= 0){
- if (p[x]> size) {
- p[x] = p[x] * (-1);
- p[0] = p[0] - ((p[x]*(-1)) + 4);
- return &p[x+1];
- }
- else if ((size > p[x]) && (p[x] != 0))
- return NULL;
- else {
- p[x] = ( size ) * (-1);
- p[0] -= (size + 4);
- return &p[x+1];
- }
- }
- else {
- if (p[x] < 0){
- x = x +1+ ((p[x]*(-1))/4);
- }
- else if (p[x] >= 0){
- x = x +1+ ((p[x])/4);
- }
- }
- }
- }
- else
- return NULL;
- }
- }
- int memory_free(void *valid_ptr)
- {
- int x=2;
- int pom = 0;
- for(;;){
- if (&p[x] == valid_ptr){
- if(p[x-1] < 0) {
- p[x-1] = p[x-1] *(-1);
- p[0] = p[0] + p[x-1] +4;
- pom = 0;
- return 0;
- }
- }
- else {
- if (p[x-1] < 0) {
- x = x + 1 + ((p[x-1]*(-1))/4);
- pom = 0;
- }
- else {
- if (p[x-1] > 0) {
- x = x + 1 + ((p[x-1])/4);
- pom=0;
- }
- }
- }
- if (pom == 1){
- return 1;
- }
- pom = 1;
- }
- }
- int memory_check(void *ptr)
- {
- return 0;
- }
- void memory_init(void *ptr, unsigned int size)
- {
- p = ptr;
- p[0] = (size -4);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement