1 /*
2  * Copyright 2014, Kevin Greenan, Tushar Gohad, All rights reserved
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice, this
11  * list of conditions and the following disclaimer in the documentation and/or
12  * other materials provided with the distribution.  THIS SOFTWARE IS PROVIDED BY
13  * THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
17  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
18  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
20  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
21  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  * liberasurecode proprocssing helpers header
25  *
26  * vi: set noai tw=79 ts=4 sw=4:
27  */
28 
29 #ifndef _ERASURECODE_HELPERS_H_
30 #define _ERASURECODE_HELPERS_H_
31 
32 #include "erasurecode_stdinc.h"
33 
34 /* ==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~== */
35 
36 #define talloc(type, num)   (type *) malloc(sizeof(type) * (num))
37 
38 /* Determine if an address is aligned to a particular boundary */
39 static inline
is_addr_aligned(unsigned long addr,int align)40 int is_addr_aligned(unsigned long addr, int align)
41 {
42     return (addr & (align - 1)) == 0;
43 }
44 
45 /*
46  * Convert an int list into a bitmap
47  * Assume the list is '-1' terminated.
48  */
49 static inline
convert_list_to_bitmap(int * list)50 unsigned long long convert_list_to_bitmap(int *list)
51 {
52     int i = 0;
53     unsigned long long bm = 0;
54 
55     while (list[i] > -1) {
56         /*
57          * TODO: Assert list[i] < 64
58          */
59         bm |= (1 << list[i]);
60         i++;
61     }
62 
63     return bm;
64 }
65 
66 /*
67  * Convert an index list int list into a bitmap
68  * is_idx_in_erasure[] needs to be allocated by the caller
69  * @returns number of idxs in error
70  */
71 static inline
convert_idx_list_to_bitvalues(int * list_idxs,int * is_idx_in_erasure,int num_idxs)72 int convert_idx_list_to_bitvalues(
73         int *list_idxs,         // input idx_list
74         int *is_idx_in_erasure, // output idx list as boolean values (1/0)
75         int num_idxs)           // total number of indexes
76 {
77     int i = 0, n = 0;
78 
79     for (i = 0; i < num_idxs; i++)
80         is_idx_in_erasure[i] = 0;
81     for (i = 0, n = 0; (list_idxs[i] > -1) && (n < num_idxs); i++, n++)
82         is_idx_in_erasure[list_idxs[i]] = 1;
83 
84     return n;
85 }
86 
87 /* ==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~== */
88 
89 void *alloc_zeroed_buffer(int size);
90 void *alloc_and_set_buffer(int size, int value);
91 void *check_and_free_buffer(void *buf);
92 void *get_aligned_buffer16(int size);
93 
94 /* ==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~==~=*=~== */
95 
96 #endif  // _ERASURECODE_HELPERS_H_
97 
98