Skip to content
This repository
Newer
Older
100644 87 lines (73 sloc) 2.765 kb
69d4044b » mbsmith
2012-04-22 Added modelines to source files.
1 /* vim: set expandtab ts=4 sw=4: */
9aa22fec » cjdelisle
2012-01-12 Offered permission to copy under gplv3 license.
2 /*
3 * You may redistribute this program and/or modify it under the terms of
4 * the GNU General Public License as published by the Free Software Foundation,
5 * either version 3 of the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
d475c9c1 » cjdelisle
2011-02-16 First commit for cjdns.
15 #include "ArrayWriter.h"
bb329314 » Caleb James DeLisle
2012-04-28 Replaced all calls to memcpy() with Bits_memcpy() and Bits_memcpyCons…
16 #include "util/Bits.h"
d475c9c1 » cjdelisle
2011-02-16 First commit for cjdns.
17
18 struct ArrayWriter_context {
19 char* beginPointer;
20 char* pointer;
21 char* endPointer;
22 int returnCode;
23 };
24
92142dd9 » cjdelisle
2011-03-13 Random stuff needed to get tinystore working.
25 static int write(const void* toWrite, size_t length, const struct Writer* writer);
26 static uint64_t bytesWritten(const struct Writer* writer);
d475c9c1 » cjdelisle
2011-02-16 First commit for cjdns.
27
28 /** @see ArrayWriter.h */
29 struct Writer* ArrayWriter_new(void* writeToBuffer,
30 size_t length,
cc3cded8 » cjdelisle
2012-01-15 Rename MemAllocator Allocator
31 const struct Allocator* allocator)
d475c9c1 » cjdelisle
2011-02-16 First commit for cjdns.
32 {
33 struct Writer* writer =
34 allocator->calloc(sizeof(struct Writer), 1, allocator);
35 struct ArrayWriter_context* context =
36 allocator->calloc(sizeof(struct ArrayWriter_context), 1, allocator);
37
38 if (context == NULL || writer == NULL) {
39 return NULL;
40 }
41
42 context->beginPointer = (char*) writeToBuffer;
43 context->pointer = (char*) writeToBuffer;
44 context->endPointer = (char*) writeToBuffer + length;
45
46 struct Writer localWriter = {
47 .context = context,
48 .write = write,
49 .bytesWritten = bytesWritten
50 };
bb329314 » Caleb James DeLisle
2012-04-28 Replaced all calls to memcpy() with Bits_memcpy() and Bits_memcpyCons…
51 Bits_memcpyConst(writer, &localWriter, sizeof(struct Writer));
d475c9c1 » cjdelisle
2011-02-16 First commit for cjdns.
52
53 return writer;
54 }
55
56 /** @see Writer->write() */
92142dd9 » cjdelisle
2011-03-13 Random stuff needed to get tinystore working.
57 static int write(const void* toWrite, size_t length, const struct Writer* writer)
d475c9c1 » cjdelisle
2011-02-16 First commit for cjdns.
58 {
59 struct ArrayWriter_context* context =
60 (struct ArrayWriter_context*) writer->context;
61
62 /* If there was a previous failure then don't allow any more writing. */
63 if (context->returnCode != 0) {
64 return context->returnCode;
65 }
66
67 /* Prove that it doesn't run off the end of the buffer or roll over. */
68 if (context->pointer + length >= context->endPointer
69 || context->pointer + length < context->pointer)
70 {
71 context->returnCode = -1;
72 return -1;
73 }
74
bb329314 » Caleb James DeLisle
2012-04-28 Replaced all calls to memcpy() with Bits_memcpy() and Bits_memcpyCons…
75 Bits_memcpy(context->pointer, toWrite, length);
d475c9c1 » cjdelisle
2011-02-16 First commit for cjdns.
76 context->pointer += length;
77
78 return 0;
79 }
80
92142dd9 » cjdelisle
2011-03-13 Random stuff needed to get tinystore working.
81 static uint64_t bytesWritten(const struct Writer* writer)
d475c9c1 » cjdelisle
2011-02-16 First commit for cjdns.
82 {
83 struct ArrayWriter_context* context =
84 (struct ArrayWriter_context*) writer->context;
85
86 return context->pointer - context->beginPointer;
87 }
Something went wrong with that request. Please try again.