| 1 |
/* |
| 2 |
* This file is part of libbluray |
| 3 |
* Copyright (C) 2009-2010 Obliter0n |
| 4 |
* Copyright (C) 2009-2010 John Stebbins |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU Lesser General Public |
| 8 |
* License as published by the Free Software Foundation; either |
| 9 |
* version 2.1 of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This library 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 GNU |
| 14 |
* Lesser General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public |
| 17 |
* License along with this library. If not, see |
| 18 |
* <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#ifndef MACRO_H_ |
| 22 |
#define MACRO_H_ |
| 23 |
|
| 24 |
#include <stdlib.h> /* free() */ |
| 25 |
|
| 26 |
#define MKINT_BE16(X) ( (X)[0] << 8 | (X)[1] ) |
| 27 |
#define MKINT_BE24(X) ( (X)[0] << 16 | (X)[1] << 8 | (X)[2] ) |
| 28 |
#define MKINT_BE32(X) ( (unsigned)((X)[0]) << 24 | (X)[1] << 16 | (X)[2] << 8 | (X)[3] ) |
| 29 |
#define X_FREE(X) ( free(X), X = NULL ) |
| 30 |
|
| 31 |
#define BD_MIN(a,b) ((a)<(b)?(a):(b)) |
| 32 |
#define BD_MAX(a,b) ((a)>(b)?(a):(b)) |
| 33 |
|
| 34 |
/* |
| 35 |
* |
| 36 |
*/ |
| 37 |
|
| 38 |
// endian safe fetch |
| 39 |
|
| 40 |
//#define FETCH4(X) (uint32_t)(((uint32_t)(X)[0]<<24)|(X)[1]<<16|(X)[2]<<8|(X)[3]) |
| 41 |
static inline uint32_t FETCH4(const void *pv) { |
| 42 |
const uint8_t *p = pv; |
| 43 |
return (uint32_t)p[0] << 24 | p[1] << 16 | p[2] << 8 | p[3]; |
| 44 |
} |
| 45 |
|
| 46 |
//#define FETCHU2(X) (uint32_t)((uint16_t)(((X)[0]<<8)|(X)[1])) |
| 47 |
static inline uint32_t FETCHU2(const void *pv) { |
| 48 |
const uint8_t *p = pv; |
| 49 |
return (uint32_t)p[0] << 8 | p[1]; |
| 50 |
} |
| 51 |
|
| 52 |
// Sign extended version |
| 53 |
//#define FETCHS2(X) (int32_t)((int16_t)(((X)[0]<<8)|(X)[1])) |
| 54 |
static inline int32_t FETCHS2(const void *pv) { |
| 55 |
return (int32_t)(int16_t)FETCHU2(pv); |
| 56 |
} |
| 57 |
|
| 58 |
//#define STORE2(X, Y) (X)[1]=(((Y))&0xff);(X)[0]=(((Y)>>8)&0xff); |
| 59 |
static inline void STORE2(void *pv, uint32_t v) { |
| 60 |
uint8_t *p = pv; |
| 61 |
p[1] = v; |
| 62 |
p[0] = v >> 8; |
| 63 |
} |
| 64 |
//#define STORE4(X, Y) (X)[3]=((Y)&0xff);(X)[2]=(((Y)>>8)&0xff);(X)[1]=(((Y)>>16)&0xff);(X)[0]=(((Y)>>24)&0xff); |
| 65 |
static inline void STORE4(void *pv, uint32_t v) { |
| 66 |
uint8_t *p = pv; |
| 67 |
STORE2(p + 2, v); |
| 68 |
STORE2(p, v >> 16); |
| 69 |
} |
| 70 |
//#define STORE8(X, Y) (X)[7]=((Y)&0xff);(X)[6]=(((Y)>>8)&0xff);(X)[5]=(((Y)>>16)&0xff);(X)[4]=(((Y)>>24)&0xff);(X)[3]=(((Y)>>32)&0xff);(X)[2]=(((Y)>>40)&0xff);(X)[1]=(((Y)>>48)&0xff);(X)[0]=(((Y)>>56)&0xff); |
| 71 |
static inline void STORE8(void *pv, uint64_t v) { |
| 72 |
uint8_t *p = pv; |
| 73 |
STORE4(p + 4, v); |
| 74 |
STORE4(p, v >> 32); |
| 75 |
} |
| 76 |
|
| 77 |
#define BD_MAX_SSIZE ((int64_t)(((size_t)-1)>>1)) |
| 78 |
|
| 79 |
/* |
| 80 |
* automatic cast from void* (malloc/calloc/realloc) |
| 81 |
*/ |
| 82 |
|
| 83 |
#ifdef __cplusplus |
| 84 |
|
| 85 |
template <typename T> |
| 86 |
class auto_cast_wrapper |
| 87 |
{ |
| 88 |
public: |
| 89 |
template <typename R> friend auto_cast_wrapper<R> auto_cast(const R& x); |
| 90 |
template <typename U> operator U() { return static_cast<U>(p); } |
| 91 |
|
| 92 |
private: |
| 93 |
auto_cast_wrapper(const T& x) : p(x) {} |
| 94 |
auto_cast_wrapper(const auto_cast_wrapper& o) : p(o.p) {} |
| 95 |
|
| 96 |
auto_cast_wrapper& operator=(const auto_cast_wrapper&); |
| 97 |
|
| 98 |
const T& p; |
| 99 |
}; |
| 100 |
|
| 101 |
template <typename R> |
| 102 |
auto_cast_wrapper<R> auto_cast(const R& x) |
| 103 |
{ |
| 104 |
return auto_cast_wrapper<R>(x); |
| 105 |
} |
| 106 |
|
| 107 |
# define calloc(n,s) auto_cast(calloc(n,s)) |
| 108 |
# define malloc(s) auto_cast(malloc(s)) |
| 109 |
# define realloc(p,s) auto_cast(realloc(p,s)) |
| 110 |
# define refcnt_realloc(p,s) auto_cast(refcnt_realloc(p,s)) |
| 111 |
#endif /* __cplusplus */ |
| 112 |
|
| 113 |
|
| 114 |
#endif /* MACRO_H_ */ |