반응형
반응형
반응형
예제 코드 : code warrier용


1. VC++로 확인 Test 예제 
#include <stdio.h>
#include <stdlib.h>

int flash_open(char *str) { printf("[ Flash Open : %s ]\n", str) ; return 0 ; }
int flash_read() { printf("[ Flash Read ]\n") ; return 0 ; }
int flash_write() { printf("[ Flash Write ]\n") ; return 0 ; }
int flash_close() { printf("[ Flash Close ]\n") ; return 0 ; }
int flash_ioctl() { printf("[ Flash Ioctl ]\n") ; return 0 ; }

int serial_open(char *str) { printf("[ Serial Open : %s ]\n", str) ; return 0 ;}
int serial_read() { printf("[ Serial Read ]\n") ; return 0 ; }
int serial_write() { printf("[ Serial Write ]\n") ; return 0 ; }
int serial_close() { printf("[ Serial Close ]\n") ; return 0 ; }
int serial_ioctl() { printf("[ Serial Ioctl ]\n") ; return 0 ; }


typedef struct _device_operations
{
char *dev_name ;
int M_num ;
int (*open)() ;
int (*close)() ;
int (*ioctl)() ;
int (*read)() ;
int (*write)() ;
} device_operations;

typedef struct _device_info
{
int  flag ;
int  minor ;
int  mode ;
char *inbuf ;
char *str ;
int  buf_size ;
device_operations *device_op ;
} device_info;

device_operations flash_operation = { "Flash", 10, 
flash_open, flash_close, flash_ioctl, flash_read, flash_write } ;
device_operations serial_operation = { "serial", 15, 
serial_open, serial_close, serial_ioctl, serial_read, serial_write } ;


device_operations *device_oper_table[20] = { '\0' };
device_info   device_info_table[40] = { '\0' };

int  register_operation(int dev_num, device_operations *pdop) ;
int  unregister_operation(int dev_num, device_operations *pdop) ;

int  open(device_info *info, int dev_num, char *str) 
{
int u ;
for(u=0 ; u<40 ; u++)
if(device_info_table[u].flag == 0)
break ;
device_info_table[u] = *info ;
device_info_table[u].flag = 1 ;
device_info_table[u].str = str ;
device_info_table[u].device_op = device_oper_table[dev_num] ;
device_info_table[u].device_op->open(str) ;
return 0 ;
}

int read(int dev_num, int minor)
{
int u ;
for(u=0 ; u<40 ; u++)
if(device_info_table[u].minor == minor)
if(device_info_table[u].device_op->M_num == dev_num) {
printf("[ %s : %s ]\t", 
device_info_table[u].inbuf, device_info_table[u].str) ;
device_info_table[u].device_op->read() ;
return 0 ;
}
return -1 ;
}

int write(int dev_num, int minor)
{
int u ;
for(u=0 ; u<40 ; u++)
if(device_info_table[u].minor == minor)
if(device_info_table[u].device_op->M_num == dev_num) {
printf("[ %s : %s ]\t", 
device_info_table[u].inbuf, device_info_table[u].str) ;   
device_info_table[u].device_op->write() ;
return 0 ;
}
return -1 ;
}

int close(int dev_num, int minor)
{
int u ;
for(u=0 ; u<40 ; u++)
if(device_info_table[u].minor == minor)
if(device_info_table[u].device_op->M_num == dev_num) {
printf("[ %s : %s ]\t", 
device_info_table[u].inbuf, device_info_table[u].str) ;    
device_info_table[u].device_op->close() ;
device_info_table[u].flag = 0 ;
return 0 ;
}
return -1 ;
}

 


void main()
{
device_info flash0 = { 0, 0, 0x7f23, "Flash 0", 0, } ;
device_info flash1 = { 0, 1, 0xff46, "Flash 1", 0, } ;
device_info serial0 = { 0, 0, 0x45f2, "Serial 0", 0, } ; 
device_info serial1 = { 0, 1, 0x65ea, "Serial 1", 0, } ;

register_operation(10, &flash_operation) ;  
register_operation(15, &serial_operation) ;

open(&flash0, 10, "Arminia-209") ;  
open(&flash1, 10, "GL-209") ;  
open(&serial0, 15, "Ellias-404") ;  
open(&serial1, 15, "Deillous-404") ;
printf("\n") ;
read(15, 1) ;    read(10, 0) ;
write(10, 1) ;
close(15, 0) ;

unregister_operation(10, '\0') ;
unregister_operation(15, '\0') ;
}


int  register_operation(int dev_num, device_operations *pdop)
{
device_oper_table[dev_num] = pdop ;
return 0 ;
}

int  unregister_operation(int dev_num, device_operations *pdop)
{
device_oper_table[dev_num] = pdop ;
return 0 ;
}




반응형
반응형
www.elayer.co.kr 자료실의 2번 자료를 보시오

#include <stdio.h>

#define Function list_entry

#define list_entry(ptr, type, member) \
((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))

typedef struct _sData
{
char str[12];
int num[3];
char index;
short int snum[3];
} sData;

void main()
{
sData *pm, m = {"Arminia", {100, 200, 300}, 'A', {10, 20, 30}};

printf("&m : [%x]\n", &m);
printf("[%x]\t[%x]\n[%x]\t[%x]\n\n", &m.str, &m.num, &m.index, &m.snum);

pm = Function(&m.index, sData, index);
printf("[%s]\t[%d]\t[%d]\n\n", pm->str, pm->num[2], pm->snum[1]);

printf("&m : [%x]\n", Function(&m.str, sData, str));
printf("&m : [%x]\n", Function(&m.num, sData, num));
printf("&m : [%x]\n", Function(&m.index, sData, index));
printf("&m : [%x]\n\n", Function(&m.snum, sData, snum));

}

공유받은 코드 : 
list.c

#define __KERNEL__
#define prefetch(x) 1
#define inline

#include <stdio.h>
#include <stdlib.h>

#include "list.h"


struct t_struct
{
struct list_head list ;
int val ;
} ;

#define T_SIZE (sizeof(struct t_struct))

struct t_struct *gg ;

void init_t(void)
{
if((gg=malloc(T_SIZE)) == NULL)
printf("[ Error ]\n") ;
INIT_LIST_HEAD(&gg->list) ;
}

void print_all(void)
{
struct list_head *temp ;
printf("------------\n") ;
list_for_each(temp, &gg->list) {
struct t_struct *k = list_entry(temp, struct t_struct, list) ;
printf("[ K = %d ]\n", k->val) ;
}
}


#define typeof(x) struct t_struct

void print_all2(void)
{
struct t_struct *temp ;
printf("------------\n") ;
list_for_each_entry(temp, &gg->list, list) {
printf("[ K = %d ]\n", temp->val) ;
}
}

void main()
{
struct t_struct *a, *b ;

if((a = malloc(T_SIZE)) == NULL)
printf("[ Error ]\n") ;
if((b = malloc(T_SIZE)) == NULL)
printf("[ Error ]\n") ;

init_t() ;
a->val = 111 ; b->val = 222 ;
print_all() ;

if(list_empty(&gg->list))
printf("[ Empty ]\n") ;

list_add(&a->list, &gg->list) ;
print_all() ;
list_add(&b->list, &gg->list) ;
print_all() ;
list_del(&b->list) ;
print_all() ;
free(a) ;
free(b) ;
free(gg) ;
}



list.h
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H

#if defined(__KERNEL__) || defined(_LVM_H_INCLUDE)

// #include <linux/prefetch.h>

/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/

struct list_head 
{
struct list_head *next, *prev;
};

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name) \
        struct list_head name = LIST_HEAD_INIT(name)

#define INIT_LIST_HEAD(ptr) do { \
        (ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)


/*
* Insert a new entry between two known consecutive entries. 
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/

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);
}

/*
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
__list_add(new, head->prev, head);
}

/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
next->prev = prev;
prev->next = next;
}

/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is in an undefined state.
*/
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = (void *) 0;
entry->prev = (void *) 0;
}

/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry); 
}

/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next);
list_add(list, head);
}

/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
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);
}

/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(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;
}

/**
* list_splice - join two lists
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(struct list_head *list, struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
}

/**
* list_splice_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* The list at @list is reinitialised
*/
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);
}
}

/**
* list_entry - get the struct for this entry
* @ptr:        the &struct list_head pointer.
* @type:       the type of the struct this is embedded in.
* @member:     the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
        ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))

/**
* list_for_each        -       iterate over a list
* @pos:        the &struct list_head to use as a loop counter.
* @head:       the head for your list.
*/
#define list_for_each(pos, head) \
        for (pos = (head)->next, prefetch(pos->next); pos != (head); \
                pos = pos->next, prefetch(pos->next))
/**
* list_for_each_prev   -       iterate over a list backwards
* @pos:        the &struct list_head to use as a loop counter.
* @head:       the head for your list.
*/
#define list_for_each_prev(pos, head) \
        for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
                pos = pos->prev, prefetch(pos->prev))
                
/**
* list_for_each_safe 
* iterate over a list safe against removal of list entry
* @pos:        the &struct list_head to use as a loop counter.
* @n:          another &struct list_head to use as temporary storage
* @head:       the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
        for (pos = (head)->next, n = pos->next; pos != (head); \
                pos = n, n = pos->next)

/**
* list_for_each_entry  
* iterate over list of given type
* @pos:        the type * to use as a loop counter.
* @head:       the head for your list.
* @member:     the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member)                          \
        for (pos = list_entry((head)->next, typeof(*pos), member),      \
                     prefetch(pos->member.next);                        \
             &pos->member != (head);                                    \
             pos = list_entry(pos->member.next, typeof(*pos), member),  \
                     prefetch(pos->member.next))

#endif /* __KERNEL__ || _LVM_H_INCLUDE */

#endif







반응형
반응형
VC++ 6.0 에서
Project/Settings/C++/Category/Listing Files선택 후 Listing File Types를 적절히 선택해 주면 
Compile시 Debug폴더에 asm파일이 생성된다. 

CodeWorrier에서
Compiler후에 마우스 우클릭 후 Deassembly를 클릭하면 나옴
반응형
반응형
일반적인 함수를 구현할 때 
참조형 반환타입을 선언하게 되면
compiler는 warning을 발생시킨다.
local var를 참조했기 때문이다. 
실행시 문제가 되지 않는다 하더라도 언제 문제가 생길지 모를 일이다.

operator = 을 재구현 할 때에도 마찬가지이다. 
data & operator + (type 어쩌고) 이런 식이 되면 warning이 발생한다.
하지만 operator = (type &)을 선언해주고 나면 이 문제가 해결이 된다.
해당 객체를 대입할 때 대입연산자인 operator = 을 사용해서 대입이 되면 참조 문제가 해결이 된다.

cf.
참조 반환타입을 사용하는 이유는
실제 asm코드로 변환해서 살펴보았을 때 
callee함수가 반환시에 data형을 2번 객체생성을 해서 복사하는 것을 볼 수 있다.(강의 내용중에 있었음)
이 때 실행시간의 load가 걸리고 이것을 방지하기 위해 pointer를 사용하면 이런 객체 복사가 이루어 지지않음
하지만 local var를 대입할 경우 함수 영역이 사라지고 나면 compiler에  local var의 포인터는 관리가 되지 않아 
소멸 될 수 있기 때문에 위험성이 있다. 그래서 일반적인 경우는 반환타입을 사용할 수 없다. 

책에서 480 쪽에 비슷한 내용이 있는 것 같음

반응형

+ Recent posts