반응형
쓰레드 동기화와 Critical Section 제어




반응형
반응형
반응형
반응형
반응형
반응형
예제 코드 : 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 쪽에 비슷한 내용이 있는 것 같음

반응형
반응형
project에 cpp파일 및 c 파일이 동시에 있을 경우 c파일의 함수를 extern할 때는 extern "C"를 붙인다

이유는 cpp은 compiler에서  이름을 바꿔서 asm코드를 만든다
이유는 함수 overloading을 위해서인데 이 때문에
cpp파일에서 extern 으로 함수를 선언했을 경우에도 cpp규칙에 의해서 함수이름을 name@@YAHHHH@z등으로 바꾸게 된다.
하지만 extern "C"를 선언하게 되면 이름을 바꾸지 않고 name을 그대로 사용하기 때문에
다른 파일에 있는 c function도 사용 가능하다

mm.cpp

#include <stdio.h>

extern "C" int add(int x, int y, int z, int k);

int add(int x, int y)
{
int k = 0;
k = x + y;
return k;
}

int add(int x, int y, int z)
{
int k = 0;
k = x + y + z;
return k;
}


void main()
{
int a = 100, b = 20, c = 30, d = 10, e;
e = add(a,b);
e = add(a, b, c);
e = add(a, b, c, d);
}

mm2.c

int add(int x, int y, int z, int k)
{
int m;
m = x + y + z + k;
return m;
}

반응형
반응형
.cpp파일로 

1단계 : 
#include <stdio.h>

typedef struct _sData
{
int num ;
char str[100] ;
} sData ;


void init(sData *pm, char *pstr) ;
void add(sData *pm, char *pstr) ;
void out(sData *pm) ;
void add_int(sData *pm, int num) ;
int number(sData *pm) ;
void array(sData *pm) ;
int check(sData *pm, char ch) ;
void sub(sData *pm, char ch) ;

void main()
{
sData data = { 0, "\0" } ;
sData obj = { 0, "\0" } ;
int k ;

printf("[ Data fun ]\n") ;
init(&data, "CArA") ;
add(&data, "a7A") ; out(&data) ;

k = number(&data) ; printf("k : [ %d ]\n", k) ;
array(&data) ; out(&data) ;
printf("[ Num : 701450 ]\n") ;
add_int(&data, 701450) ; out(&data) ;

k = check(&data, 'A') ; printf("k : [ %d ]\n", k) ;
sub(&data, 'A') ; out(&data) ;

printf("\n\n[ Obj fun ]\n") ;
init(&obj, "Ellias") ; out(&obj) ;
}


int check(sData *pm, char ch)
{
int u, d ;
for(u=0, d=0 ; pm->str[u] !='\0' ;u++)
if(pm->str[u] == ch)
d++ ;
return d ;
}

void sub(sData *pm, char ch)
{
int u, d ;
for(u=0, d=0 ; pm->str[u]!='\0' ; u++, d++)
((pm->str[u]==ch)?(d--):(pm->str[d]=pm->str[u])) ;
pm->str[d]='\0' ;
pm->num=d ;
}

void add_int(sData *pm, int num)
{
int n = num ;
while(num>10) {
int u ;
for(u=1 ; n>10 ; n=n/10, u = u*10) ;
pm->str[pm->num++] = n + '0' ;
num = num - n*u ;
if((!((u/10 <= num) && (num < u)))&&((num!=0)))
pm->str[pm->num++] = '0' ;
n = num ;
}
pm->str[pm->num++] = num + '0';
pm->str[pm->num] = '\0' ;
}

int number(sData *pm)
{
int u, d ;
for(u=0, d=0 ; u<pm->num ; u++)
if(('0'<=pm->str[u])&&(pm->str[u]<='9'))
d = d * 10 + (pm->str[u]-'0') ;
return d ;
}

void array(sData *pm)
{
char temp ;
int u ;
for(u=0 ; u<pm->num-1 ; u++) {
int d ;
for(d=1 ; d<pm->num-u ; d++)
if(pm->str[d-1] > pm->str[d]) {
temp = pm->str[d-1] ;
pm->str[d-1] = pm->str[d] ;
pm->str[d] = temp ;
}
}
}

void init(sData *pm, char *pstr)
{
int u ;
for(u=0 ; pstr[u]!='\0' ; pm->str[u]=pstr[u++]) ;
pm->str[u]='\0' ;
pm->num=u ;
}

void add(sData *pm, char *pstr)
{
int u, n ;
for(u=0, n=pm->num ; pstr[u]!='\0' ; u++, n++)
pm->str[n]=pstr[u] ;
pm->str[n]='\0' ;
pm->num=n ;
}

void out(sData *pm)
{
printf("[ Num ] : %d\t[ Str ] : %s\n", pm->num, pm->str) ;
}



2단계 : 
C++의 구조체에는 함수를 포함할 수 있으므로 함수를 구조체 안에 넣었음
그리고 scope연산자를 이용해서 함수들의 scope를 sData로 지정

void sData::init(char *pstr)을 compiler는 void sData::init(sData * this, char *pstr)로 인식한다.
m.init("ABC")를 실제로 compiler는 m.init(&m, "ABC")로 인식

#include <stdio.h>

typedef struct _sData
{
int num ;
char str[100] ;
void init(char *pstr) ;
void add(char *pstr) ;
void out() ;
void add_int(int num) ;
int number() ;
void array() ;
int check(char ch) ;
void sub(char ch) ;
} sData ;



void main()
{
sData m = { 0, "\0" } ;
sData n = { 0, "\0" } ;
int k ;

printf("[ Data fun ]\n") ;
m.init("CArA") ;
m.add("a7A") ; m.out() ;

k = m.number() ; printf("k : [ %d ]\n", k) ;
m.array() ; m.out() ;
printf("[ Num : 701450 ]\n") ;
m.add_int(701450) ; m.out() ;

k = m.check('A') ; printf("k : [ %d ]\n", k) ;
m.sub('A') ; m.out() ;

printf("\n\n[ Obj fun ]\n") ;
n.init("Ellias") ; n.out() ;
}


int sData::check(char ch)
{
int u, d ;
for(u=0, d=0 ; str[u] !='\0' ;u++)
if(str[u] == ch)
d++ ;
return d ;
}

void sData::sub(char ch)
{
int u, d ;
for(u=0, d=0 ; str[u]!='\0' ; u++, d++)
((str[u]==ch)?(d--):(str[d]=str[u])) ;
str[d]='\0' ;
num=d ;
}

void sData::add_int(int num)
{
int n = num ;
while(num>10) {
int u ;
for(u=1 ; n>10 ; n=n/10, u = u*10) ;
str[this->num++] = n + '0' ;
num = num - n*u ;
if((!((u/10 <= num) && (num < u)))&&((num!=0)))
str[this->num++] = '0' ;
n = num ;
}
str[this->num++] = num + '0';
str[this->num] = '\0' ;
}

int sData::number()
{
int u, d ;
for(u=0, d=0 ; u<num ; u++)
if(('0'<=str[u])&&(str[u]<='9'))
d = d * 10 + (str[u]-'0') ;
return d ;
}

void sData::array()
{
char temp ;
int u ;
for(u=0 ; u<num-1 ; u++) {
int d ;
for(d=1 ; d<num-u ; d++)
if(str[d-1] > str[d]) {
temp = str[d-1] ;
str[d-1] = str[d] ;
str[d] = temp ;
}
}
}

void sData::init(char *pstr)
{
int u ;
for(u=0 ; pstr[u]!='\0' ; str[u]=pstr[u++]) ;
str[u]='\0' ;
num=u ;
}

void sData::add(char *pstr)
{
int u, n ;
for(u=0, n=num ; pstr[u]!='\0' ; u++, n++)
str[n]=pstr[u] ;
str[n]='\0' ;
num=n ;
}

void sData::out()
{
printf("[ Num ] : %d\t[ Str ] : %s\n", num, str) ;
}


3단계 : 
기본성격이 public인 struct에서는 수납된 data의 접근을 막을 방법이 없음
data의 접근을 제한할 수 있는 class로 바꿔주겠음

#include <stdio.h>

class sData
{
protected : 
int num ;
char str[100] ;

public:
void init(char *pstr) ;
void add(char *pstr) ;
void out() ;
void add_int(int num) ;
int number() ;
void array() ;
int check(char ch) ;
void sub(char ch) ;
};

void main()
{
sData m;
sData n;
int k ;
printf("[ Data fun ]\n") ;
m.init("CArA") ;
m.add("a7A") ; m.out() ;
k = m.number() ; printf("k : [ %d ]\n", k) ;
m.array() ; m.out() ;
printf("[ Num : 701450 ]\n") ;
m.add_int(701450) ; m.out() ;

k = m.check('A') ; printf("k : [ %d ]\n", k) ;
m.sub('A') ; m.out() ;
printf("\n\n[ Obj fun ]\n") ;
n.init("Ellias") ; n.out() ;
}


4단계 : 
이제부터는 class설계 잘하면 된다 ㅎ
C++의 기능들 잘 활용해가면서 ㅎ



























반응형
반응형
union을 이용
그안에 있는 pointer선언형으로 compiler가 자동으로 변환해주는 효과를 얻을 수 있음

http://lxr.linux.no/linux+v2.6.13/include/linux/fs.h 에서도 union사용용법을 확인할 수 있음

1단계 : 
#include <stdio.h>

typedef struct _sData
{
int num ;
char str[100] ;
} sData ;

void init(sData *pm, char *pstr) ;
void add(sData *pm, char *pstr) ;
void out(sData *pm) ;
void add_int(sData *pm, int num) ;
int number(sData *pm) ;
void array(sData *pm) ;
int check(sData *pm, char ch) ;
void sub(sData *pm, char ch) ;

void main()
{
sData data = { 0, "\0" } ;
sData obj = { 0, "\0" } ;
int k ;

printf("\n\n[ Obj fun ]\n") ;
n.init(&n, "Ellias") ;
n.out(&n) ;
printf("[ Data fun ]\n") ;
m.init(&m, "CArA") ;
m.add(&m, "a7A") ; m.out(&m) ;

k = m.number(&m) ; printf("k : [ %d ]\n", k) ;
m.array(&m) ; m.out(&m) ;
printf("[ Num : 701450 ]\n") ;
m.add_int(&m, 701459) ; m.out(&m) ;

k = m.check(&m, 'A') ; printf("k : [ %d ]\n", k) ;
m.sub(&m, 'A') ; m.out(&m) ;

printf("\n\n[ Obj fun ]\n") ;
n.init(&n, "Ellias") ; n.out(&n) ;
}

int check(sData *pm, char ch)
{
int u, d ;
for(u=0, d=0 ; pm->str[u] !='\0' ;u++)
if(pm->str[u] == ch)
d++ ;
return d ;
}

void sub(sData *pm, char ch)
{
int u, d ;
for(u=0, d=0 ; pm->str[u]!='\0' ; u++, d++)
((pm->str[u]==ch)?(d--):(pm->str[d]=pm->str[u])) ;
pm->str[d]='\0' ;
pm->num=d ;
}

void add_int(sData *pm, int num)
{
int n = num ;
while(num>10) {
int u ;
for(u=1 ; n>10 ; n=n/10, u = u*10) ;
pm->str[pm->num++] = n + '0' ;
num = num - n*u ;
if((!((u/10 <= num) && (num < u)))&&((num!=0)))
pm->str[pm->num++] = '0' ;
n = num ;
}
pm->str[pm->num++] = num + '0';
pm->str[pm->num] = '\0' ;
}

int number(sData *pm)
{
int u, d ;
for(u=0, d=0 ; u<pm->num ; u++)
if(('0'<=pm->str[u])&&(pm->str[u]<='9'))
d = d * 10 + (pm->str[u]-'0') ;
return d ;
}

void array(sData *pm)
{
char temp ;
int u ;
for(u=0 ; u<pm->num-1 ; u++) {
int d ;
for(d=1 ; d<pm->num-u ; d++)
if(pm->str[d-1] > pm->str[d]) {
temp = pm->str[d-1] ;
pm->str[d-1] = pm->str[d] ;
pm->str[d] = temp ;
}
}
}

void init(sData *pm, char *pstr)
{
int u ;
for(u=0 ; pstr[u]!='\0' ; pm->str[u]=pstr[u++]) ;
pm->str[u]='\0' ;
pm->num=u ;
}

void add(sData *pm, char *pstr)
{
int u, n ;
for(u=0, n=pm->num ; pstr[u]!='\0' ; u++, n++)
pm->str[n]=pstr[u] ;
pm->str[n]='\0' ;
pm->num=n ;
}

void out(sData *pm)
{
printf("[ Num ] : %d\t[ Str ] : %s\n", pm->num, pm->str) ;
}

2단계 :  
#include <stdio.h>

typedef struct _sData
{
int num ;
char str[100] ;
void (* init)(struct _sData *pm, char *) ; //함수에 대한 선두번지를 갖는 pointer선언
void (* add)(struct _sData *pm, char *) ;
void (* out)(struct _sData *pm) ;
void (* add_int)(struct _sData *pm, int) ;
int (* number)(struct _sData *pm) ;
void (* array)(struct _sData *pm) ;
int (* check)(struct _sData *pm, char) ;
void (* sub)(struct _sData *pm, char) ;
} sData ;

void init(sData *pm, char *pstr) ;
void add(sData *pm, char *pstr) ;
void out(sData *pm) ;
void add_int(sData *pm, int num) ;
int number(sData *pm) ;
void array(sData *pm) ;
int check(sData *pm, char ch) ;
void sub(sData *pm, char ch) ;

void main()
{
sData m = { 0, "\0" , init, add, out, add_int, number, array, check, sub} ;
sData n = { 0, "\0" , init, add, out, add_int, number, array, check, sub} ;
int k ;

printf("\n\n[ Obj fun ]\n") ;
n.init(&n, "Ellias") ;
n.out(&n) ;
printf("[ Data fun ]\n") ;
m.init(&m, "CArA") ;
m.add(&m, "a7A") ; m.out(&m) ;

k = m.number(&m) ; printf("k : [ %d ]\n", k) ;
m.array(&m) ; m.out(&m) ;
printf("[ Num : 701450 ]\n") ;
m.add_int(&m, 701459) ; m.out(&m) ;

k = m.check(&m, 'A') ; printf("k : [ %d ]\n", k) ;
m.sub(&m, 'A') ; m.out(&m) ;

printf("\n\n[ Obj fun ]\n") ;
n.init(&n, "Ellias") ; n.out(&n) ;
}
함수는 동일


3단계 : 
#include <stdio.h>

typedef struct { 
void *pfun[8]; 
} pData;

typedef struct _sData
{
int num ;
char str[100] ;
union {
struct {
void (* init)(struct _sData *pm, char *) ; //함수에 대한 선두번지를 갖는 pointer선언
void (* add)(struct _sData *pm, char *) ;
void (* out)(struct _sData *pm) ;
void (* add_int)(struct _sData *pm, int) ;
int (* number)(struct _sData *pm) ;
void (* array)(struct _sData *pm) ;
int (* check)(struct _sData *pm, char) ;
void (* sub)(struct _sData *pm, char) ;
};
pData fun_list;
};
} sData ;

void init(sData *pm, char *pstr) ;
void add(sData *pm, char *pstr) ;
void out(sData *pm) ;
void add_int(sData *pm, int num) ;
int number(sData *pm) ;
void array(sData *pm) ;
int check(sData *pm, char ch) ;
void sub(sData *pm, char ch) ;

const pData fun_list = {init, add, out, add_int, number, array, check, sub} ;

void main()
{
sData m = { 0, "\0"} ;
sData n = { 0, "\0"} ;
int k ;

m.fun_list = n.fun_list = fun_list;
printf("[ Data fun ]\n") ;
m.init(&m, "CArA") ; m.add(&m, "a7A") ; m.out(&m) ; k = m.number(&m) ; printf("k : [ %d ]\n", k) ; m.array(&m) ; m.out(&m) ; printf("[ Num : 701450 ]\n") ; m.add_int(&m, 701459) ; m.out(&m) ; k = m.check(&m, 'A') ; printf("k : [ %d ]\n", k) ; m.sub(&m, 'A') ; m.out(&m) ; printf("\n\n[ Obj fun ]\n") ; n.init(&n, "Ellias") ; n.out(&n) ;
}


4단계 : 

#include <stdio.h>

typedef struct { 
void *pfun[8]; 
} pData;

typedef union _uFun{
struct {
void (* init)(struct _sData *pm, char *) ; //함수에 대한 선두번지를 갖는 pointer선언
void (* add)(struct _sData *pm, char *) ;
void (* out)(struct _sData *pm) ;
void (* add_int)(struct _sData *pm, int) ;
int (* number)(struct _sData *pm) ;
void (* array)(struct _sData *pm) ;
int (* check)(struct _sData *pm, char) ;
void (* sub)(struct _sData *pm, char) ;
};
pData fun_list;
} uFun;

typedef struct _sData
{
int num ;
char str[100];
const uFun * const op;
} sData ;

void init(sData *pm, char *pstr) ;
void add(sData *pm, char *pstr) ;
void out(sData *pm) ;
void add_int(sData *pm, int num) ;
int number(sData *pm) ;
void array(sData *pm) ;
int check(sData *pm, char ch) ;
void sub(sData *pm, char ch) ;

const uFun fun_list = {init, add, out, add_int, number, array, check, sub} ;

void main()
{
sData m = { 0, "\0", &fun_list} ;
sData n = { 0, "\0", &fun_list} ;
int k ;

//m.fun_list = n.fun_list = fun_list;
printf("[ Data fun ]\n") ;
m.op->init(&m, "CArA") ;
m.op->add(&m, "a7A") ; m.op->out(&m) ;

k = m.op->number(&m) ; printf("k : [ %d ]\n", k) ;
m.op->array(&m) ; m.op->out(&m) ;
m.op->add_int(&m, 7045) ; m.op->out(&m) ;

k = m.op->check(&m, 'A') ; printf("k : [ %d ]\n", k) ;
m.op->sub(&m, 'A') ; m.op->out(&m) ;

printf("\n\n[ Obj fun ]\n") ;
n.op->init(&n, "Ellias") ; n.op->out(&n) ;
}




5단계 : 
warning 제거


#include <stdio.h>

typedef struct { 
void *pfun[8]; 
} pData;

typedef struct {
void (* init)(struct _sData *pm, char*) ; //함수에 대한 선두번지를 갖는 pointer선언
void (* add)(struct _sData *pm, char *) ;
void (* out)(struct _sData *pm) ;
void (* add_int)(struct _sData *pm, int) ;
int (* number)(struct _sData *pm) ;
void (* array)(struct _sData *pm) ;
int (* check)(struct _sData *pm, char) ;
void (* sub)(struct _sData *pm, char) ;
} sData_operations;

typedef union _uFun{
sData_operations sData_op;
pData fun_list;
} uFun;

typedef struct _sData
{
int num ;
char str[100];
const sData_operations * const op;
} sData ;

void init(sData *pm, char *pstr) ;
void add(sData *pm, char *pstr) ;
void out(sData *pm) ;
void add_int(sData *pm, int num) ;
int number(sData *pm) ;
void array(sData *pm) ;
int check(sData *pm, char ch) ;
void sub(sData *pm, char ch) ;

const uFun fun_list = {init, add, out, add_int, number, array, check, sub} ;

void main()
{
sData m = { 0, "\0", &fun_list.sData_op} ;
sData n = { 0, "\0", &fun_list.sData_op} ;
int k ;

//m.fun_list = n.fun_list = fun_list;
printf("[ Data fun ]\n") ;
m.op->init(&m, "CArA") ;
m.op->add(&m, "a7A") ; m.op->out(&m) ;

k = m.op->number(&m) ; printf("k : [ %d ]\n", k) ;
m.op->array(&m) ; m.op->out(&m) ;
m.op->add_int(&m, 7045) ; m.op->out(&m) ;

k = m.op->check(&m, 'A') ; printf("k : [ %d ]\n", k) ;
m.op->sub(&m, 'A') ; m.op->out(&m) ;


printf("\n\n[ Obj fun ]\n") ;
n.op->init(&n, "Ellias") ; n.op->out(&n) ;
}


6단계 : 
compiler의 특성을 이용해 매개변수 list를 생략 가능, 필요하다면 이렇게도 가능
device별 연결될 매개인자 list가 다를 때 활용 할 수 있음
C언어의 관용을 최대한 활용한 예임

typedef struct { 
void *pfun[8]; 
} pData;

typedef struct {
void (* init)() ; //함수에 대한 선두번지를 갖는 pointer선언
void (* add)() ;
void (* out)() ;
void (* add_int)() ;
int (* number)() ;
void (* array)() ;
int (* check)() ;
void (* sub)() ;
} sData_operations;

typedef union _uFun{
sData_operations sData_op;
pData fun_list;
} uFun;

typedef struct _sData
{
int num ;
char str[100];
const sData_operations * const op;
} sData ;

void init() ;
void add() ;
void out() ;
void add_int() ;
int number() ;
void array() ;
int check() ;
void sub() ;











반응형

+ Recent posts