R
rkalyankumar77
Hi,
I see strange problem while I run the below code on Windows using gcc
(mingw 3.4.5) & MS-visual studio compiler 2003 (cl compiler) -
compiles ok. Both are failing for some referenced memory being passed
to the routines add_head/add_tail and the macro add_list which
couldn't be read. However this code works well with the gcc compiler
under cygwin on windows and gcc compiler on any Linux system.
Are there any issues with portability of code to windows? I am unable
to guess where this is going wrong & why.
Thanks for your suggestions/solution in advance.
PS: I haven't coded for freeing memory that's malloc'd.
list.h
----------
#ifndef _K_LIST_H_
#define _K_LIST_H_
struct list_head
{
struct list_head *prev,*next;
};
#define LIST_INIT(name) {&(name),&(name)}
#define LIST(name) \
struct list_head name = LIST_INIT(name)
#ifndef __GNUC__
#define list_entry(PTR,TYPE,FIELD) ((TYPE*)((char*) PTR -
offsetof(TYPE,FIELD)))
#else /*GCC specific implementation*/
#define list_entry(PTR,TYPE,FIELD) ({\
const typeof( ((TYPE *) 0)->FIELD) *__mptr = (PTR);\
(TYPE*) ((char*) __mptr - offsetof(TYPE,FIELD));})
#endif
void init_list_head(struct list_head *list);
void
init_list_head(struct list_head *list)
{
list->next = list;
list->prev = list;
}
extern void add_tail(struct list_head *tail, struct list_head
*node);
extern void add_head(struct list_head *head, struct list_head
*node);
#define list_for_each(head,node) \
for((node) = (head)->next;(node) != (head) && (node);(node) = (node)-
list.c
------------
#include "list.h"
#define add_list(N1,N2,N3) { \
do { \
struct list_head *__new = (struct list_head*) (N1); \
struct list_head *__prev = (struct list_head*) (N2); \
struct list_head *__next = (struct list_head*) (N3); \
__next->prev = (struct list_head*)__new;\
__new->next =(struct list_head*) __next;\
__new->prev = (struct list_head*)__prev;\
__prev->next = (struct list_head*)__new;\
} while(0); \
}
void
add_head(struct list_head *head,struct list_head *node)
{
add_list(node,head,head->next);
}
void
add_tail(struct list_head *head,struct list_head *node)
{
add_list(node,head->prev,head);
}
struct int_list
{
int data;
struct list_head head;
};
int
main(void)
{
struct list_head *val;
struct int_list *ilist,*inode;
int i;
ilist = malloc(sizeof(struct list_head));
init_list_head(&ilist->head);
for(i = 0; i < 10; i++) {
inode = malloc(sizeof(struct list_head));
inode->data = i + 1;
printf("Adding %d to list\n",inode->data);
add_head(&ilist->head,&inode->head);
}
list_for_each(&ilist->head,val) {
inode = list_entry(val,struct int_list,head);
printf("%d ",inode->data);
}
printf("\n");
system("pause");
return 0;
}
I see strange problem while I run the below code on Windows using gcc
(mingw 3.4.5) & MS-visual studio compiler 2003 (cl compiler) -
compiles ok. Both are failing for some referenced memory being passed
to the routines add_head/add_tail and the macro add_list which
couldn't be read. However this code works well with the gcc compiler
under cygwin on windows and gcc compiler on any Linux system.
Are there any issues with portability of code to windows? I am unable
to guess where this is going wrong & why.
Thanks for your suggestions/solution in advance.
PS: I haven't coded for freeing memory that's malloc'd.
list.h
----------
#ifndef _K_LIST_H_
#define _K_LIST_H_
struct list_head
{
struct list_head *prev,*next;
};
#define LIST_INIT(name) {&(name),&(name)}
#define LIST(name) \
struct list_head name = LIST_INIT(name)
#ifndef __GNUC__
#define list_entry(PTR,TYPE,FIELD) ((TYPE*)((char*) PTR -
offsetof(TYPE,FIELD)))
#else /*GCC specific implementation*/
#define list_entry(PTR,TYPE,FIELD) ({\
const typeof( ((TYPE *) 0)->FIELD) *__mptr = (PTR);\
(TYPE*) ((char*) __mptr - offsetof(TYPE,FIELD));})
#endif
void init_list_head(struct list_head *list);
void
init_list_head(struct list_head *list)
{
list->next = list;
list->prev = list;
}
extern void add_tail(struct list_head *tail, struct list_head
*node);
extern void add_head(struct list_head *head, struct list_head
*node);
#define list_for_each(head,node) \
for((node) = (head)->next;(node) != (head) && (node);(node) = (node)-
#endifnext)
list.c
------------
#include "list.h"
#define add_list(N1,N2,N3) { \
do { \
struct list_head *__new = (struct list_head*) (N1); \
struct list_head *__prev = (struct list_head*) (N2); \
struct list_head *__next = (struct list_head*) (N3); \
__next->prev = (struct list_head*)__new;\
__new->next =(struct list_head*) __next;\
__new->prev = (struct list_head*)__prev;\
__prev->next = (struct list_head*)__new;\
} while(0); \
}
void
add_head(struct list_head *head,struct list_head *node)
{
add_list(node,head,head->next);
}
void
add_tail(struct list_head *head,struct list_head *node)
{
add_list(node,head->prev,head);
}
struct int_list
{
int data;
struct list_head head;
};
int
main(void)
{
struct list_head *val;
struct int_list *ilist,*inode;
int i;
ilist = malloc(sizeof(struct list_head));
init_list_head(&ilist->head);
for(i = 0; i < 10; i++) {
inode = malloc(sizeof(struct list_head));
inode->data = i + 1;
printf("Adding %d to list\n",inode->data);
add_head(&ilist->head,&inode->head);
}
list_for_each(&ilist->head,val) {
inode = list_entry(val,struct int_list,head);
printf("%d ",inode->data);
}
printf("\n");
system("pause");
return 0;
}