I need to make a shared memory segment that contains the nodes using this structure that I created
struct a_list
{
struct list_head list;
unsigned long* val;
char* str;
char state;
};
I tried to find answers on the stack, but they implemented Node, unlike my structure, which is a list with nodes inside
I also have this buffer and the add method that I used to add to this list (using malloc), but I know that for shared memory I cannot do this
struct buffer
{
struct a_list* buffer[BUFFER_SIZE];
int in;
int out;
};
static void append(struct a_list* ptr,const char* str, unsigned long val)
{
struct a_list* tmp;
tmp = (struct a_list*)malloc(sizeof(struct a_list));
tmp->str = (char *)malloc(strlen(str)+1);
strcpy(tmp->str, str);
tmp->val = (unsigned long*)malloc(sizeof(unsigned long));
memcpy(tmp->val, &val, sizeof(unsigned long));
tmp->index = (int *)malloc(sizeof(int));
memcpy(tmp->index, &buffer_p->in, sizeof(int));
tmp->address = (struct a_list*)malloc(sizeof(struct a_list*));
memcpy(tmp->address, &fileQueue, sizeof(struct a_list*));
if(list_empty(&fileQueue.list)){
list_add_tail( &(tmp->list), &(ptr->list) );
head = list_entry(fileQueue.list.next,struct a_list, list);
}
else{
list_add_tail( &(tmp->list), &(ptr->list) );
}
buffer_p->buffer[buffer_p->in] = tmp->address;
buffer_p->in = (buffer_p->in+1) % BUFFER_SIZE;
}
and basically I'm trying to initialize everything with
if ((shmid = shmget(key, (BUFFER_SIZE+2)*sizeof(int), IPC_CREAT | 0666)) < 0) {
perror("shmget");
exit(1);
}
if ((shm = shmat(shmid, NULL, 0)) < 0) {
perror("shmat");
exit(1);
}
buffer_p = (struct buffer*) shm;
buffer_p->in = 0;
buffer_p->out = 0;
, , im , malloc'd ( ), , , Beej Guide , , (, , ). , , , . list_head a_list (fileQueue) , .
EDIT: list_head, ,
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
struct list_head {
struct list_head *next, *prev;
};
struct list_head name = LIST_HEAD_INIT(name)
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
static inline void __list_add(struct list_head *new,
struct list_head *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
static inline void __list_splice(struct list_head *list,
struct list_head *head)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
struct list_head *at = head->next;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
}
static inline void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
}
static inline void list_splice_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head);
INIT_LIST_HEAD(list);
}
}
container_of(ptr, type, member)
for (pos = (head)->next; pos != (head); \
pos = pos->next)
for (pos = (head)->next; pos != (head); pos = pos->next)
for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
pos = pos->prev)
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
for (pos = list_entry((head)->next, typeof(*pos), member); \
pos->member != (head); \
pos = list_entry(pos->member->next, typeof(*pos), member))
for (pos = list_entry((head)->prev, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member))
((pos) ? : list_entry(head, typeof(*pos), member))
for (pos = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
for (pos = list_entry(pos->member.next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
for (pos = list_entry((head)->prev, typeof(*pos), member), \
n = list_entry(pos->member.prev, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.prev, typeof(*n), member))
struct hlist_head {
struct hlist_node *first;
};
struct hlist_node {
struct hlist_node *next, **pprev;
};
static inline int hlist_unhashed(const struct hlist_node *h)
{
return !h->pprev;
}
static inline int hlist_empty(const struct hlist_head *h)
{
return !h->first;
}
static inline void __hlist_del(struct hlist_node *n)
{
struct hlist_node *next = n->next;
struct hlist_node **pprev = n->pprev;
*pprev = next;
if (next)
next->pprev = pprev;
}
static inline void hlist_del(struct hlist_node *n)
{
__hlist_del(n);
n->next = LIST_POISON1;
n->pprev = LIST_POISON2;
}
static inline void hlist_del_init(struct hlist_node *n)
{
if (n->pprev) {
__hlist_del(n);
INIT_HLIST_NODE(n);
}
}
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
struct hlist_node *first = h->first;
n->next = first;
if (first)
first->pprev = &n->next;
h->first = n;
n->pprev = &h->first;
}
static inline void hlist_add_before(struct hlist_node *n,
struct hlist_node *next)
{
n->pprev = next->pprev;
n->next = next;
next->pprev = &n->next;
*(n->pprev) = n;
}
static inline void hlist_add_after(struct hlist_node *n,
struct hlist_node *next)
{
next->next = n->next;
n->next = next;
next->pprev = &n->next;
if(next->next)
next->next->pprev = &next->next;
}
for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
pos = pos->next)
for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
pos = n)
for (pos = (head)->first; \
pos && ({ prefetch(pos->next); 1;}) && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
for (pos = (pos)->next; \
pos && ({ prefetch(pos->next); 1;}) && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
for (; pos && ({ prefetch(pos->next); 1;}) && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
for (pos = (head)->first; \
pos && ({ n = pos->next; 1; }) && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = n)