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/>.
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
);
36 proc
= (proc_t
*) &proc_kernel
;
40 char *ptr
= (char *) umalloc (proc
, sizeof (cache_t
) + len
);
44 cache_t
*cache
= (cache_t
*) ptr
;
49 cache
->magic
= CACHE_MAGIC
;
50 cache
->prealloc
= prealloc
;
55 memcpy (&cache
->data
, buf
, len
);
61 return (cache_t
*) ((char *) cache
);
64 cache_t
*cache_add (char *buf
, unsigned len
)
69 proc_t
*proc
= proc_find (_curr_task
);
72 proc
= (proc_t
*) &proc_kernel
;
74 unsigned l
= cache_last
->limit
;
75 char *p
= (char *) &cache_last
->data
;
77 if (!cache_last
->prealloc
) {
81 cache_last
= (cache_t
*) urealloc (proc
, p
- sizeof (cache_t
) + 4, l
+ len
+ sizeof (cache_t
));
88 p
= (char *) &cache_last
->data
;
90 memcpy (p
+ l
, buf
, len
);
92 cache_last
->limit
+= len
;
94 memcpy (p
+ cache_last
->curr
, buf
, len
);
96 cache_last
->curr
+= len
;
101 cache_t
*cache_read ()
106 proc_t
*proc
= proc_find (_curr_task
);
108 cache_t
*cache
= (cache_t
*) cache_last
;
110 if (cache
->magic
!= CACHE_MAGIC
)
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
);
126 proc
= (proc_t
*) &proc_kernel
;
128 /*if (cache->magic != CACHE_MAGIC)
133 ufree (proc
, (cache_t
*) ((char *) cache
));
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 ()