Skip to content

Commit

Permalink
Linux Memory Manager Integrated with the Project
Browse files Browse the repository at this point in the history
  • Loading branch information
sachinites committed Feb 16, 2020
1 parent a3fc648 commit fcfc247
Show file tree
Hide file tree
Showing 36 changed files with 1,397 additions and 113 deletions.
2 changes: 1 addition & 1 deletion BitOp/bitarr.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ print_bit_array(bit_array_t *bitarr){
int
main(int argc, char **argv){

bit_array_t *arr = calloc(1, sizeof(bit_array_t));
bit_array_t *arr = XCALLOC(1, bit_array_t);
init_bit_array(arr, 15);
set_bit(arr, 11);
set_bit(arr, 21);
Expand Down
15 changes: 8 additions & 7 deletions LinkedList/LinkedListApi.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
#include <memory.h>
#include "LinkedListApi.h"
#include <assert.h>
#include "../LinuxMemoryManager/uapi_mm.h"

ll_t* init_singly_ll(){
return calloc(1, sizeof(ll_t));
return XCALLOC(1, ll_t);
}

singly_ll_node_t* singly_ll_init_node(void* data){
singly_ll_node_t* node = calloc(1, sizeof(singly_ll_node_t));
singly_ll_node_t* node = XCALLOC(1, singly_ll_node_t);
node->data = data;
return node;
}
Expand Down Expand Up @@ -92,14 +93,14 @@ singly_ll_delete_node(ll_t *ll, singly_ll_node_t *node){
node->data = node->next->data;
temp = node->next;
node->next = node->next->next;
free(temp);
XFREE(temp);
DEC_NODE_COUNT_SINGLY_LL(ll);
return 0;
}

/* if node is the only node in LL*/
if(ll->node_count == 1 && GET_HEAD_SINGLY_LL(ll) == node){
free(node);
XFREE(node);
GET_HEAD_SINGLY_LL(ll) = NULL;
DEC_NODE_COUNT_SINGLY_LL(ll);
return 0;
Expand All @@ -113,7 +114,7 @@ singly_ll_delete_node(ll_t *ll, singly_ll_node_t *node){
}

trav->next = NULL;
free(node);
XFREE(node);
DEC_NODE_COUNT_SINGLY_LL(ll);
return 0;
}
Expand Down Expand Up @@ -276,7 +277,7 @@ delete_singly_ll(ll_t *ll){
*next = GET_NEXT_NODE_SINGLY_LL(head);

do{
free(head);
XFREE(head);
head = next;
if(next)
next = GET_NEXT_NODE_SINGLY_LL(next);
Expand Down Expand Up @@ -327,7 +328,7 @@ singly_ll_delete_node_by_data_ptr(ll_t *ll, void *data){
return;

singly_ll_remove_node(ll, list_node);
free(list_node);
XFREE(list_node);
list_node = NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion LinkedList/LinkedListApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void singly_ll_add_ordered_data(ll_t *ll, void *data);
else if(node_ptr && prev){ \
prev->next = node_ptr->next; \
} \
free(node_ptr); \
XFREE(node_ptr); \
list_ptr->node_count--; \
node_ptr = NULL;}

Expand Down
21 changes: 21 additions & 0 deletions LinuxMemoryManager/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CC=gcc
CFLAGS=-g
TARGET:testapp.exe libmm.a
OUTFILES=testapp.exe libmm.a
EXTERNAL_LIBS=
OBJS=gluethread/glthread.o mm.o

testapp.exe:testapp.o ${OBJS}
${CC} ${CFLAGS} testapp.o ${OBJS} -o testapp.exe ${EXTERNAL_LIBS}
testapp.o:testapp.c
${CC} ${CFLAGS} -c testapp.c -o testapp.o
gluethread/glthread.o:gluethread/glthread.c
${CC} ${CFLAGS} -c -I gluethread gluethread/glthread.c -o gluethread/glthread.o
mm.o:mm.c
${CC} ${CFLAGS} -c mm.c -o mm.o
libmm.a:${OBJ}
ar rs libmm.a ${OBJ}
clean:
rm -f testapp.o
rm -f ${OUTFILES}
rm -f ${OBJS}
12 changes: 12 additions & 0 deletions LinuxMemoryManager/css.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef __CSS__
#define __CSS__

#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"

#endif
104 changes: 104 additions & 0 deletions LinuxMemoryManager/gluethread/glthread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* =====================================================================================
*
* Filename: glthread.h
*
* Description: This file defines the Data structure and APIs for Glue thread
*
* Version: 1.0
* Created: Monday 12 March 2018 02:01:51 IST
* Revision: 1.0
* Compiler: gcc
*
* Author: Er. Abhishek Sagar, Networking Developer (AS), sachinites@gmail.com
* Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present)
*
* This file is part of the SPFComputation distribution (https://github.com/sachinites).
* Copyright (c) 2017 Abhishek Sagar.
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* =====================================================================================
*/

#ifndef __GLUETHREAD__
#define __GLUETHREAD__

typedef struct _glthread{

struct _glthread *left;
struct _glthread *right;
} glthread_t;

void
glthread_add_next(glthread_t *base_glthread, glthread_t *new_glthread);

void
glthread_add_before(glthread_t *base_glthread, glthread_t *new_glthread);

void
remove_glthread(glthread_t *glthread);

void
init_glthread(glthread_t *glthread);

void
glthread_add_last(glthread_t *base_glthread, glthread_t *new_glthread);

#define IS_GLTHREAD_LIST_EMPTY(glthreadptr) \
((glthreadptr)->right == 0 && (glthreadptr)->left == 0)

#define GLTHREAD_TO_STRUCT(fn_name, structure_name, field_name, glthreadptr) \
static inline structure_name * fn_name(glthread_t *glthreadptr){ \
return (structure_name *)((char *)(glthreadptr) - (char *)&(((structure_name *)0)->field_name)); \
}

/* delete safe loop*/
/*Normal continue and break can be used with this loop macro*/

#define BASE(glthreadptr) ((glthreadptr)->right)

#define ITERATE_GLTHREAD_BEGIN(glthreadptrstart, glthreadptr) \
{ \
glthread_t *_glthread_ptr = NULL; \
glthreadptr = BASE(glthreadptrstart); \
for(; glthreadptr!= NULL; glthreadptr = _glthread_ptr){ \
_glthread_ptr = (glthreadptr)->right;

#define ITERATE_GLTHREAD_END(glthreadptrstart, glthreadptr) \
}}

#define GLTHREAD_GET_USER_DATA_FROM_OFFSET(glthreadptr, offset) \
(void *)((char *)(glthreadptr) - offset)

void
delete_glthread_list(glthread_t *base_glthread);

unsigned int
get_glthread_list_count(glthread_t *base_glthread);

void
glthread_priority_insert(glthread_t *base_glthread,
glthread_t *glthread,
int (*comp_fn)(void *, void *),
int offset);


#if 0
void *
gl_thread_search(glthread_t *base_glthread,
void *(*thread_to_struct_fn)(glthread_t *),
void *key,
int (*comparison_fn)(void *, void *));

#endif
#endif /* __GLUETHREAD__ */
Loading

0 comments on commit fcfc247

Please sign in to comment.