팁
[Tip] 어떤 클래스의 멤버 변수의 주소를 이용해 객체의 주소 알아오기.
Binceline
2014. 4. 13. 01:05
pintos project를 하다가 발견한 기법이다.
struct list_elem { struct list_elem *prev; /* Previous list element. */ struct list_elem *next; /* Next list element. */ }; struct thread { /* Owned by thread.c. */ tid_t tid; /* Thread identifier. */ enum thread_status status; /* Thread state. */ char name[16]; /* Name (for debugging purposes). */ uint8_t *stack; /* Saved stack pointer. */ int priority; /* Priority. */ struct list_elem allelem; /* List element for all threads list. */ /* Shared between thread.c and synch.c. */ struct list_elem elem; /* List element. */ #ifdef USERPROG /* Owned by userprog/process.c. */ uint32_t *pagedir; /* Page directory. */ #endif /* Owned by thread.c. */ unsigned magic; /* Detects stack overflow. */ }; #define list_entry(LIST_ELEM, STRUCT, MEMBER) \n ((STRUCT *) ((uint8_t *) &(LIST_ELEM)->next - offsetof (STRUCT, MEMBER.next))) // 사용 예 struct thread *t = list_entry (e, struct thread, allelem);
반응형