Fixed ZDE build - missing header file
[ZeXOS.git] / kernel / core / cache.c
blob331bb7621c13cccee3677e7e29a596b9ffaf7fb7
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2010 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <system.h>
22 #include <string.h>
23 #include <config.h>
24 #include <build.h>
25 #include <cache.h>
26 #include <task.h>
27 #include <proc.h>
29 static cache_t *cache_last;
31 cache_t *cache_create (char *buf, unsigned len, unsigned char prealloc)
33 proc_t *proc = proc_find (_curr_task);
35 if (!proc)
36 proc = (proc_t *) &proc_kernel;
37 #ifdef ARCH_i386
38 paging_disable ();
39 #endif
40 char *ptr = (char *) umalloc (proc, sizeof (cache_t) + len);
41 #ifdef ARCH_i386
42 paging_enable ();
43 #endif
44 cache_t *cache = (cache_t *) ptr;
46 if (!cache)
47 goto ret;
49 cache->magic = CACHE_MAGIC;
50 cache->prealloc = prealloc;
51 cache->limit = len;
52 cache->curr = 0;
54 if (len && buf)
55 memcpy (&cache->data, buf, len);
56 else
57 cache->data = '\0';
59 cache_last = cache;
60 ret:
61 return (cache_t *) ((char *) cache);
64 cache_t *cache_add (char *buf, unsigned len)
66 if (!cache_last)
67 return 0;
69 proc_t *proc = proc_find (_curr_task);
71 if (!proc)
72 proc = (proc_t *) &proc_kernel;
74 unsigned l = cache_last->limit;
75 char *p = (char *) &cache_last->data;
77 if (!cache_last->prealloc) {
78 #ifdef ARCH_i386
79 paging_disable ();
80 #endif
81 cache_last = (cache_t *) urealloc (proc, p - sizeof (cache_t) + 4, l + len + sizeof (cache_t));
82 #ifdef ARCH_i386
83 paging_enable ();
84 #endif
85 if (!cache_last)
86 return 0;
88 p = (char *) &cache_last->data;
90 memcpy (p + l, buf, len);
92 cache_last->limit += len;
93 } else
94 memcpy (p + cache_last->curr, buf, len);
96 cache_last->curr += len;
98 return cache_last;
101 cache_t *cache_read ()
103 if (!cache_last)
104 return 0;
106 proc_t *proc = proc_find (_curr_task);
108 cache_t *cache = (cache_t *) cache_last;
110 if (cache->magic != CACHE_MAGIC)
111 return 0;
113 cache_last = 0;
115 if (!proc)
116 proc = (proc_t *) &proc_kernel;
118 return (cache_t *) ((char *) cache);
121 int cache_close (cache_t *cache)
123 proc_t *proc = proc_find (_curr_task);
125 if (!proc)
126 proc = (proc_t *) &proc_kernel;
128 /*if (cache->magic != CACHE_MAGIC)
129 return 0;*/
130 #ifdef ARCH_i386
131 paging_disable ();
132 #endif
133 ufree (proc, (cache_t *) ((char *) cache));
134 #ifdef ARCH_i386
135 paging_enable ();
136 #endif
137 return 1;
140 int cache_closebyptr (void *ptr)
142 char *cache = (char *) ptr - sizeof (cache_t) + 4;
144 return cache_close ((cache_t *) cache);
147 unsigned int init_cache ()
149 cache_last = 0;