Skip to content

Commit 9771150

Browse files
committed
creating MLD objects
1 parent 2f13c86 commit 9771150

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

app.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ int main(int argc, char **argv) {
8888
};
8989
REG_STRUCT(struct_db, student_t, stud_fields);
9090

91-
/* Verify the structure database by printing it */
91+
/* Step 4: Verify the structure database by printing it */
9292
print_structure_db(struct_db);
93+
94+
95+
/* Working with object database */
96+
/* Step 1: Initialize a new Object database */
97+
object_db_t *object_db = calloc(1, sizeof(object_db_t));
98+
object_db->struct_db = struct_db;
99+
100+
/* Step 2: Create some sample objects, equivalent to standard
101+
calloc(1, sizeof(student_t) */
102+
student_t *studA = xcalloc(object_db, "student_t", 1);
103+
student_t *studB = xcalloc(object_db, "student_t", 1);
104+
emp_t *empA = xcalloc(object_db, "emp_t", 2);
105+
106+
print_object_db(object_db);
107+
return 0;
93108
}

mld.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,84 @@ int add_structure_to_struct_db(struct_db_t *struct_db, struct_db_rec_t *struct_r
9595
struct_db->count++;
9696
return 0;
9797
}
98+
99+
static struct_db_rec_t
100+
*struct_db_look_up(struct_db_t *struct_db,
101+
char *struct_name) {
102+
struct_db_rec_t *head = struct_db->head;
103+
if(!head) return NULL;
104+
for(; head; head = head->next) {
105+
if(strncmp(head->struct_name, struct_name, MAX_STRUCTURE_NAME_SIZE) == 0)
106+
return head;
107+
}
108+
return NULL;
109+
}
110+
111+
static object_db_rec_t
112+
*object_db_look_up(object_db_t *object_db, void *ptr) {
113+
object_db_rec_t *head = object->head;
114+
if(!head) return NULL;
115+
for (; head; head = head->next) {
116+
if(head->ptr == ptr)
117+
return head;
118+
}
119+
return NULL;
120+
}
121+
122+
/* To work with objects */
123+
static void add_object_to_object_db(object_db_t *object_db,
124+
void *ptr,
125+
int units,
126+
struct_db_rec_t *struct_rec,
127+
mld_boolean_t is_root) {
128+
object_db_rec_t *obj_rec = object_db_look_up(object_db, ptr);
129+
/* Don't add same object twice */
130+
assert(!obj_rec);
131+
132+
obj_rec = calloc(1, sizeof(object_db_rec_t));
133+
134+
obj_rec->next = NULL;
135+
obj_rec->ptr = ptr;
136+
obj_rec->units = units;
137+
obj_rec->struct_rec = struct_rec;
138+
obj_rec->is_visited = MLD_FALSE;
139+
obj_rec->is_root = is_root;
140+
141+
object_db_rec_t *head = object_db->head;
142+
143+
if(!head) {
144+
object_db->head = obj_rec;
145+
object->next = NULL;
146+
object_db->count++;
147+
return;
148+
}
149+
obj_rec->next = head;
150+
object_db->head = obj_rec;
151+
object_db->count++;
152+
}
153+
154+
void *xcalloc(object_db_t *object_db, char *struct_name, int uints) {
155+
struct_db_rec_t *struct_rec = struct_db_look_up(object_db->struct_db, struct_name);
156+
assert(struct_rec);
157+
158+
void *ptr = calloc(uints, struct_rec->ds_size);
159+
add_object_to_object_db(object_db, ptr, uints, strutc_rec, MLD_FALSE);
160+
return ptr;
161+
}
162+
163+
/* Print functions for object database */
164+
void print_object_rec(object_db_rec_t *obj_rec, int i) {
165+
if(!obj_rec) return;
166+
printf(ANSI_COLOR_MAGENTA "--------------------------|\n" ANSI_COLOR_RESET);
167+
printf(ANSI_COLOR_YELLOW "%-3d ptr = %-10p | next = %-10p | units = %-4d | struct_name = %-10s | is_root = %s |\n" ANSI_COLOR_RESET, i, obj_rec->ptr, obj_rec->next, obj_rec->units, obj_rec->struct_rec->struct_name, obj_rec->is_root ? "TRUE" : "FALSE");
168+
printf(ANSI_COLOR_MAGENTA "--------------------------|\n" ANSI_COLOR_RESET);
169+
}
170+
171+
void print_object_db(object_db_t *object_db) {
172+
object_db_rec_t *head = object_db->head;
173+
unsigned int i = 0;
174+
printf(ANSI_COLOR_CYAN "Printing OBJECT DATABASE\n");
175+
for(; head; head->next) {
176+
print_object_rec(head, i++);
177+
}
178+
}

0 commit comments

Comments
 (0)