LMMS
Loading...
Searching...
No Matches
system.c
Go to the documentation of this file.
1/*
2 Copyright 2011-2020 David Robillard <d@drobilla.net>
3
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
7
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
17#define _POSIX_C_SOURCE 200809L /* for posix_memalign and posix_fadvise */
18
19#include "system.h"
20
21#include "serd_config.h"
22#include "serd_internal.h"
23
24#if USE_POSIX_FADVISE && USE_FILENO
25# include <fcntl.h>
26#endif
27
28#ifdef _WIN32
29# include <malloc.h>
30#endif
31
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37FILE*
38serd_fopen(const char* path, const char* mode)
39{
40 FILE* fd = fopen(path, mode);
41 if (!fd) {
42 fprintf(
43 stderr, "error: failed to open file %s (%s)\n", path, strerror(errno));
44 return NULL;
45 }
46
47#if USE_POSIX_FADVISE && USE_FILENO
48 posix_fadvise(fileno(fd), 0, 0, POSIX_FADV_SEQUENTIAL);
49#endif
50 return fd;
51}
52
53void*
54serd_malloc_aligned(const size_t alignment, const size_t size)
55{
56#if defined(_WIN32)
57 return _aligned_malloc(size, alignment);
58#elif USE_POSIX_MEMALIGN
59 void* ptr = NULL;
60 const int ret = posix_memalign(&ptr, alignment, size);
61 return ret ? NULL : ptr;
62#else
63 (void)alignment;
64 return malloc(size);
65#endif
66}
67
68void*
73
74void
75serd_free_aligned(void* const ptr)
76{
77#ifdef _WIN32
78 _aligned_free(ptr);
79#else
80 free(ptr);
81#endif
82}
#define NULL
Definition CarlaBridgeFormat.cpp:30
#define SERD_PAGE_SIZE
Definition serd_internal.h:27
png_structrp int mode
Definition png.h:1139
FILE * serd_fopen(const char *path, const char *mode)
Open a file configured for fast sequential reading.
Definition system.c:38
void * serd_malloc_aligned(const size_t alignment, const size_t size)
Allocate a buffer aligned to alignment bytes.
Definition system.c:54
void * serd_allocate_buffer(const size_t size)
Allocate an aligned buffer for I/O.
Definition system.c:69
void serd_free_aligned(void *const ptr)
Free a buffer allocated with an aligned allocation function.
Definition system.c:75
ulg size
Definition extract.c:2350
#define void
Definition unzip.h:396
char * malloc()