Repository navigation
Expand file tree
/
Copy pathenv.h
More file actions
129 lines (84 loc) · 2.82 KB
/
Copy pathenv.h
File metadata and controls
129 lines (84 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef ENV_H
#define ENV_H
#include "types.h"
#include "config.h"
#include <stdarg.h>
#ifndef NULL
#define NULL ((void *)0)
#endif
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((word) &((TYPE *)0)->MEMBER)
#endif
#define LIST_TO_STRUCT(TYPE, MEMBER, LIST) ((TYPE *)(((byte *)LIST) - offsetof(TYPE, MEMBER)))
/* allocate memory */
void *memory_alloc(word size);
/* free memory */
void memory_free(void *mem);
/* copy data from inside memory to inside memory */
void *memory_copy(void *dst, const void *src, word len);
/* copy data from outside memory to inside memory */
int memory_copy_from_outside(void *dst, const void *src, word len);
/* copy data from inside memory to outside memory */
int memory_copy_to_outside(void *dst, const void *src, word len);
/* set the value of an inside memory */
void *memory_set(void *str, int ch, word num);
/* compare two strings */
int string_compare(const char *s1, const char *s2);
/* copy a string to a buffer */
char *string_copy(char *s1, const char *s2);
/* get a string's length */
int string_len(const char *s);
/* find an external function by its name */
void *find_external_function(const byte *name);
/* functions to print to a standard output */
#ifdef __KERNEL__
#include <linux/kernel.h>
#include <linux/spinlock.h>
#include <linux/sched.h>
#include <linux/wait.h>
#define output_string(...) printk(__VA_ARGS__)
#define KPLUGS_IOCTYPE (154)
#define define_io(num, cont) _IOWR(KPLUGS_IOCTYPE, num, cont)
#else
#include <stdio.h>
#define output_string(...) printf(__VA_ARGS__)
/* the user mode version is just for testing, AND IS NOT THREAD SAFE: */
typedef word spinlock_t;
typedef word atomic_t;
#define spin_lock_init(lock)
#define spin_lock_irqsave(lock, flags) ((flags) = 0)
#define spin_unlock_irqrestore(lock, flags)
#define atomic_set(atom, val) do { (*(atom))=val; } while (0)
#define atomic_inc(atom) do { ++(*(atom)); } while (0)
inline int atomic_dec_and_test(atomic_t *atom);
typedef word wait_queue_head_t;
#define init_waitqueue_head(wq)
#define wait_event_interruptible(wq, cond) 0
#define wake_up_interruptible(wq)
#define define_io(num, cont) num
#endif
#include "memory.h"
#ifndef PAGE_SIZE
#define PAGE_SIZE (0x1000)
#endif
#ifndef BITS_PER_BYTE
#define BITS_PER_BYTE (8)
#endif
/* works only with elements which there size is a power of two! */
#define ROUNDUP(num, elem) (((num) + (elem) - 1) & (~((elem) - 1)))
#define ROUNDDOWN(num, elem) ((num) & (~((elem) - 1)))
#ifndef MAX
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#endif
#define ERROR_PRINT(n) DEBUG_PRINT("ERROR %d: in line %d of file \"%s\"\n", n, __LINE__, __FILE__)
#define ERROR(n) if (n) { \
ERROR_PRINT(n); \
return n; \
} else return 0
#define ERROR_CLEAN(n) if (n) { \
ERROR_PRINT(n); \
} err = n; goto clean
#define CHECK_ERROR(err) do { \
if ((err) < 0) { goto clean; } \
} while (0)
#endif