mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-17 16:32:11 +01:00
SPDX is an ISO standard for describing licenses in a computer readable format, which is useful when applying license compliance tools and the like to source code. In addition to ensuring that the Wuffs C distribution is properly annotated to the benefit of projects using Wuffs and SPDX-supporting tools, add it everywhere else where there's a license in this repo for consistency, too.
69 lines
2.5 KiB
C
69 lines
2.5 KiB
C
// Copyright 2017 The Wuffs Authors.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
|
|
// option. This file may not be copied, modified, or distributed
|
|
// except according to those terms.
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
|
|
|
// ---------------- Memory Allocation
|
|
|
|
// The memory allocation related functions in this section aren't used by Wuffs
|
|
// per se, but they may be helpful to the code that uses Wuffs.
|
|
|
|
// wuffs_base__malloc_slice_uxx wraps calling a malloc-like function, except
|
|
// that it takes a uint64_t number of elements instead of a size_t size in
|
|
// bytes, and it returns a slice (a pointer and a length) instead of just a
|
|
// pointer.
|
|
//
|
|
// You can pass the C stdlib's malloc as the malloc_func.
|
|
//
|
|
// It returns an empty slice (containing a NULL ptr field) if (num_uxx *
|
|
// sizeof(uintxx_t)) would overflow SIZE_MAX.
|
|
|
|
static inline wuffs_base__slice_u8 //
|
|
wuffs_base__malloc_slice_u8(void* (*malloc_func)(size_t), uint64_t num_u8) {
|
|
if (malloc_func && (num_u8 <= (SIZE_MAX / sizeof(uint8_t)))) {
|
|
void* p = (*malloc_func)((size_t)(num_u8 * sizeof(uint8_t)));
|
|
if (p) {
|
|
return wuffs_base__make_slice_u8((uint8_t*)(p), (size_t)num_u8);
|
|
}
|
|
}
|
|
return wuffs_base__make_slice_u8(NULL, 0);
|
|
}
|
|
|
|
static inline wuffs_base__slice_u16 //
|
|
wuffs_base__malloc_slice_u16(void* (*malloc_func)(size_t), uint64_t num_u16) {
|
|
if (malloc_func && (num_u16 <= (SIZE_MAX / sizeof(uint16_t)))) {
|
|
void* p = (*malloc_func)((size_t)(num_u16 * sizeof(uint16_t)));
|
|
if (p) {
|
|
return wuffs_base__make_slice_u16((uint16_t*)(p), (size_t)num_u16);
|
|
}
|
|
}
|
|
return wuffs_base__make_slice_u16(NULL, 0);
|
|
}
|
|
|
|
static inline wuffs_base__slice_u32 //
|
|
wuffs_base__malloc_slice_u32(void* (*malloc_func)(size_t), uint64_t num_u32) {
|
|
if (malloc_func && (num_u32 <= (SIZE_MAX / sizeof(uint32_t)))) {
|
|
void* p = (*malloc_func)((size_t)(num_u32 * sizeof(uint32_t)));
|
|
if (p) {
|
|
return wuffs_base__make_slice_u32((uint32_t*)(p), (size_t)num_u32);
|
|
}
|
|
}
|
|
return wuffs_base__make_slice_u32(NULL, 0);
|
|
}
|
|
|
|
static inline wuffs_base__slice_u64 //
|
|
wuffs_base__malloc_slice_u64(void* (*malloc_func)(size_t), uint64_t num_u64) {
|
|
if (malloc_func && (num_u64 <= (SIZE_MAX / sizeof(uint64_t)))) {
|
|
void* p = (*malloc_func)((size_t)(num_u64 * sizeof(uint64_t)));
|
|
if (p) {
|
|
return wuffs_base__make_slice_u64((uint64_t*)(p), (size_t)num_u64);
|
|
}
|
|
}
|
|
return wuffs_base__make_slice_u64(NULL, 0);
|
|
}
|