2 * Copyright (C) 2013-2014 Intel Corporation.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * José Bollo <jose.bollo@open.eurogiciel.org>
20 * Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21 * Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
40 static int buffread(struct buffer *buffer, int fd)
42 const int size = 4096;
50 p = realloc(memory, length + size);
56 status = read(fd, memory+length, size);
58 buffer->buffer = memory;
59 buffer->length = length;
64 length = length + (int)status;
65 } else if (errno != EAGAIN && errno != EINTR) {
72 int buffer_create(struct buffer *buffer, const char *pathname)
79 result = open(pathname, O_RDONLY);
82 result = fstat(fd, &bstat);
84 length = (size_t)bstat.st_size;
85 if (bstat.st_size != (off_t)length) {
89 memory = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fd, 0);
90 if (memory != MAP_FAILED) {
91 buffer->buffer = memory;
92 buffer->length = length;
95 result = buffread(buffer, fd);
104 int buffer_destroy(struct buffer *buffer)
107 return munmap(buffer->buffer, buffer->length);
108 free(buffer->buffer);