My Project
Loading...
Searching...
No Matches
kernel_smartptr.h
Go to the documentation of this file.
1#pragma once
2#ifndef KERNEL_SMARTPTR_H
3#define KERNEL_SMARTPTR_H
4
5// 기존 코드
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <pthread.h>
10#include <netdb.h>
11#include <arpa/inet.h>
12#include <netinet/in.h>
13#include <sys/types.h>
14#include <unistd.h>
15#include <errno.h>
16#include <semaphore.h>
17#include <sys/wait.h>
18#include <fcntl.h>
19#include <stdbool.h>
20#include <stdarg.h>
21#include <dlfcn.h>
22
23#include "kernel_engine.h"
24#include "../src/ename.c.inc"
25
26#define NUM_THREADS 3
27#define MAX_STRING_SIZE 100
28
29typedef struct SmartPtr SmartPtr;
30#define CREATE_SMART_PTR(type, ...) create_smart_ptr(sizeof(type), __VA_ARGS__)
31
32static void retain(SmartPtr *sp);
33static void release(SmartPtr *sp);
34static void* thread_function(void* arg);
35
36// static pthread_mutex_t print_mutex = PTHREAD_MUTEX_INITIALIZER;
37
44typedef struct SmartPtr {
45 void *ptr;
46 int *ref_count;
47 pthread_mutex_t *mutex;
48} SmartPtr;
49
53typedef struct NetworkInfo {
54 char ip[INET_ADDRSTRLEN];
55 sa_family_t family;
57
64 struct addrinfo hints, *res;
65 NetworkInfo net_info;
66
67 memset(&hints, 0, sizeof(hints));
68 hints.ai_family = AF_INET;
69 hints.ai_socktype = SOCK_STREAM;
70 hints.ai_flags = AI_PASSIVE;
71
72 char hostname[256];
73 gethostname(hostname, sizeof(hostname));
74 if (getaddrinfo(hostname, NULL, &hints, &res) != 0) {
75 perror("getaddrinfo 실패");
76 exit(EXIT_FAILURE);
77 }
78
79 struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
80 inet_ntop(AF_INET, &(ipv4->sin_addr), net_info.ip, INET_ADDRSTRLEN);
81 net_info.family = res->ai_family;
82
83 freeaddrinfo(res);
84 return net_info;
85}
86
94SmartPtr create_smart_ptr(size_t size, ...) {
95 (void)size;
96 SmartPtr sp;
97 sp.ptr = malloc(size);
98 sp.ref_count = (int *)malloc(sizeof(int));
99 *(sp.ref_count) = 1;
100 sp.mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
101 pthread_mutex_init(sp.mutex, NULL);
102
103 va_list args;
104 va_start(args, size);
105
106 if (size == sizeof(int)) {
107 int value = va_arg(args, int);
108 *(int *)sp.ptr = value;
109 } else if (size == sizeof(char) * MAX_STRING_SIZE) {
110 const char *str = va_arg(args, const char *);
111 strncpy((char *)sp.ptr, str, MAX_STRING_SIZE);
112 }
113
114 va_end(args);
115 return sp;
116}
117
123static void retain(SmartPtr *sp) {
124 pthread_mutex_lock(sp->mutex);
125 (*(sp->ref_count))++;
126 pthread_mutex_unlock(sp->mutex);
127}
128
134static void release(SmartPtr *sp) {
135 int should_free = 0;
136
137 pthread_mutex_lock(sp->mutex);
138 (*(sp->ref_count))--;
139 safe_kernel_printf("Smart pointer released (ref_count: %d)\n", *(sp->ref_count));
140
141 if (*(sp->ref_count) == 0) {
142 should_free = 1;
143 safe_kernel_printf("Reference count is 0, freeing memory...\n");
144 }
145
146 pthread_mutex_unlock(sp->mutex);
147
148 if (should_free) {
149 free(sp->ptr);
150 sp->ptr = NULL;
151 free(sp->ref_count);
152 sp->ref_count = NULL;
153
154 pthread_mutex_destroy(sp->mutex);
155 free(sp->mutex);
156 sp->mutex = NULL;
157
158 safe_kernel_printf("Memory has been freed\n");
159 }
160}
161
168void* thread_function(void* arg) {
169 int thread_num = *((int*)arg);
170
172
173 safe_kernel_printf("Thread %d: 시작 - 로컬 IP 주소: %s\n", thread_num, net_info.ip);
174
175 sleep(1);
176
177 safe_kernel_printf("Thread %d: 종료 - 주소 패밀리: %d\n", thread_num, net_info.family);
178 return NULL;
179}
180
181
182#endif // KERNEL_SMARTPTR_H
void safe_kernel_printf(const char *format,...)
스레드 안전한 출력 함수 선언
#define MAX_STRING_SIZE
struct SmartPtr SmartPtr
SmartPtr create_smart_ptr(size_t size,...)
스마트 포인터를 생성하는 함수 (가변 인자 사용)
struct NetworkInfo NetworkInfo
네트워크 정보를 저장하는 구조체
NetworkInfo get_local_network_info()
로컬 네트워크 정보를 가져오는 함수
네트워크 정보를 저장하는 구조체
sa_family_t family
주소 패밀리 (AF_INET 등)
char ip[INET_ADDRSTRLEN]
IPv4 주소
스마트 포인터 구조체
pthread_mutex_t * mutex
뮤텍스 보호
int * ref_count
참조 카운트
void * ptr
실제 메모리를 가리킴