Assignment 3 for glthread

This commit is contained in:
2024-02-08 12:03:28 +05:30
parent 34c4f2eafa
commit 34370a7e00
12 changed files with 2568 additions and 0 deletions

BIN
Assignment3/.glthread.h.un~ Normal file

Binary file not shown.

BIN
Assignment3/.main.c.un~ Normal file

Binary file not shown.

BIN
Assignment3/exeglthread Executable file

Binary file not shown.

81
Assignment3/glthread.c Normal file
View File

@@ -0,0 +1,81 @@
#include "glthread.h"
#include <stdlib.h>
#include <stdio.h>
/*Private function to add a new_node right after curr_node*/
static void
_glthread_add_next(glthread_node_t *curr_node,
glthread_node_t *new_node){
if(!curr_node->right){
curr_node->right = new_node;
new_node->left = curr_node;
return;
}
glthread_node_t *temp = curr_node->right;
curr_node->right = new_node;
new_node->left = curr_node;
new_node->right = temp;
temp->left = new_node;
}
/*Fn to insert a new GLnode into a glthread at the first position
* in a glthread i.e. new node becomes a head
* */
void
glthread_add (glthread_t *lst, glthread_node_t *glnode){
glnode->left = NULL;
glnode->right = NULL;
if(!lst->head){
lst->head = glnode;
return;
}
glthread_node_t *head = lst->head;
_glthread_add_next(glnode, head);
lst->head = glnode;
}
static void
_remove_glthread(glthread_node_t *glnode){
if(!glnode->left){
if(glnode->right){
glnode->right->left = NULL;
glnode->right = 0;
return;
}
return;
}
if(!glnode->right){
glnode->left->right = NULL;
glnode->left = NULL;
return;
}
glnode->left->right = glnode->right;
glnode->right->left = glnode->left;
glnode->left = 0;
glnode->right = 0;
}
/*API to remove a glnode from glthread*/
void
glthread_remove(glthread_t *lst, glthread_node_t *glnode){
glthread_node_t *head = lst->head;
/*If the node being removed is the head node itself, then update the
* head*/
if(head == glnode){
lst->head = head->right;
}
_remove_glthread(glnode);
}
void
init_glthread(glthread_t *glthread, unsigned int offset){
glthread->head = NULL;
glthread->offset = offset;
}

49
Assignment3/glthread.h Normal file
View File

@@ -0,0 +1,49 @@
#ifndef __GLTHREADS__
#define __GLTHREADS__
typedef struct glthread_node_ {
struct glthread_node_ *left;
struct glthread_node_ *right;
} glthread_node_t;
typedef struct glthread_ {
glthread_node_t *head;
unsigned int offset;
} glthread_t;
void
glthread_add (glthread_t *lst, glthread_node_t *glnode);
void
glthread_remove(glthread_t *lst, glthread_node_t *glnode);
/*Iterative macro to Iterate Over GLTHhreads*/
#define ITERATE_GL_THREADS_BEGIN(lstptr, struct_type, ptr) \
{ \
glthread_node_t *_glnode = NULL, *_next = NULL; \
for(_glnode = lstptr->head; _glnode; _glnode = _next){ \
_next = _glnode->right; \
ptr = (struct_type *)((char *)_glnode - lstptr->offset);
#define ITERATE_GL_THREADS_ENDS }}
#define glthread_node_init(glnode) \
glnode->left = NULL; \
glnode->right = NULL;
void
init_glthread(glthread_t *glthread, unsigned int offset);
#define offsetof(struct_name, field_name) \
((unsigned long)&((struct_name *)0)->field_name)
#define glthread_to_object(fn_name,struct_name,field_name) \
static inline struct_name *fn_name(glthread_node_t *field_name){ \
return (struct_name *)((unsigned long)field_name - offsetof(struct_name, field_name)); \
}
#endif /* __GLTHREADS__ */

49
Assignment3/glthread.h~ Normal file
View File

@@ -0,0 +1,49 @@
#ifndef __GLTHREADS__
#define __GLTHREADS__
typedef struct glthread_node_ {
struct glthread_node_ *left;
struct glthread_node_ *right;
} glthread_node_t;
typedef struct glthread_ {
glthread_node_t *head;
unsigned int offset;
} glthread_t;
void
glthread_add (glthread_t *lst, glthread_node_t *glnode);
void
glthread_remove(glthread_t *lst, glthread_node_t *glnode);
/*Iterative macro to Iterate Over GLTHhreads*/
#define ITERATE_GL_THREADS_BEGIN(lstptr, struct_type, ptr) \
{ \
glthread_node_t *_glnode = NULL, *_next = NULL; \
for(_glnode = lstptr->head; _glnode; _glnode = _next){ \
_next = _glnode->right; \
ptr = (struct_type *)((char *)_glnode - lstptr->offset);
#define ITERATE_GL_THREADS_ENDS }}
#define glthread_node_init(glnode) \
glnode->left = NULL; \
glnode->right = NULL;
void
init_glthread(glthread_t *glthread, unsigned int offset);
#define offsetof(struct_name, field_name) \
((unsigned long)&((struct_name *)0)->field_name)
#define glthread_to_object(fn_name,struct_name,field_name) \
static inline struct_name *fn_name(glthread_node_t *field_name){ \
return (struct_name *)((unsigned int *)field_name - offsetof(struct_name, field_name)); \
}
#endif /* __GLTHREADS__ */

BIN
Assignment3/glthread.o Normal file

Binary file not shown.

82
Assignment3/main.c Normal file
View File

@@ -0,0 +1,82 @@
/*
* =====================================================================================
*
* Filename: main.c
*
* Description:
*
* Version: 1.0
* Created: 08/02/24 08:27:48 AM IST
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include "glthread.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
typedef struct emp_ {
char name[30];
unsigned int salary;
char designation[30];
unsigned int emp_id;
glthread_node_t glnode;
} emp_t ;
/*Function to print employee details*/
void
print_employee_info(emp_t *emp){
printf("Employee name = %s\n", emp->name);
printf("salary = %u\n", emp->salary);
printf("designation = %s\n", emp->designation);
printf("emp_id = %u\n", emp->emp_id);
}
glthread_to_object(glnode_to_employee,emp_t,glnode)
void
print_employee_details( glthread_node_t *glnode){
emp_t * emp = glnode_to_employee(glnode);
print_employee_info(emp);
}
int main( int argc , char** argv ){
/*Creating one employee object*/
emp_t *emp1 = calloc(1, sizeof(emp_t));
strncpy(emp1->name, "Neha", strlen("Neha"));
emp1->salary = 50000;
strncpy(emp1->designation, "HR", strlen("HR"));
emp1->emp_id = 21;
glthread_node_init((&emp1->glnode));
print_employee_details((&emp1->glnode));
return 0;
}

81
Assignment3/main.c~ Normal file
View File

@@ -0,0 +1,81 @@
/*
* =====================================================================================
*
* Filename: main.c
*
* Description:
*
* Version: 1.0
* Created: 08/02/24 08:27:48 AM IST
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include "glthread.h"
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
typedef struct emp_ {
char name[30];
unsigned int salary;
char designation[30];
unsigned int emp_id;
glthread_node_t glnode;
} emp_t ;
/*Function to print employee details*/
void
print_employee_info(emp_t *emp){
printf("Employee name = %s\n", emp->name);
printf("salary = %u\n", emp->salary);
printf("designation = %s\n", emp->designation);
printf("emp_id = %u\n", emp->emp_id);
}
void
print_employee_details( glthread_node_t *glnode){
emp_t *emp = (emp_t *)((unsigned long)glnode - offsetof(emp_t,glnode));
print_employee_info(emp);
}
int main( int argc , char** argv ){
/*Creating one employee object*/
emp_t *emp1 = calloc(1, sizeof(emp_t));
strncpy(emp1->name, "Neha", strlen("Neha"));
emp1->salary = 50000;
strncpy(emp1->designation, "HR", strlen("HR"));
emp1->emp_id = 21;
glthread_node_init((&emp1->glnode));
print_employee_details((&emp1->glnode));
return 0;
}

2225
Assignment3/main.i Normal file

File diff suppressed because it is too large Load Diff

BIN
Assignment3/main.o Normal file

Binary file not shown.

1
gl_thread Submodule

Submodule gl_thread added at 12c21f8492