LIBFMSR
0.1
|
00001 00008 /* =================================================================== 00009 Copyright (c) 2013, Henry C. H. Chen 00010 All rights reserved. 00011 00012 Redistribution and use in source and binary forms, with or without 00013 modification, are permitted provided that the following conditions are 00014 met: 00015 00016 - Redistributions of source code must retain the above copyright 00017 notice, this list of conditions and the following disclaimer. 00018 00019 - Redistributions in binary form must reproduce the above copyright 00020 notice, this list of conditions and the following disclaimer in 00021 the documentation and/or other materials provided with the 00022 distribution. 00023 00024 - Neither the name of the Chinese University of Hong Kong nor the 00025 names of its contributors may be used to endorse or promote 00026 products derived from this software without specific prior written 00027 permission. 00028 00029 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00030 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00031 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00032 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00033 HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00034 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00035 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00036 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00037 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00038 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00039 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00040 =================================================================== */ 00041 00042 00043 #ifndef LIBFMSR_FMSR_H 00044 #define LIBFMSR_FMSR_H 00045 00046 #include <stdlib.h> 00047 00048 #ifndef LIBFMSR_GF 00049 #define LIBFMSR_GF 00050 00053 typedef unsigned char gf; 00054 #endif 00055 00058 typedef struct 00059 { 00060 gf last_repaired; 00061 gf last_used; 00062 } fmsr_repair_hints; 00063 00064 00065 /* ---------------------------------------------------- */ 00066 /* | initialization (call first before doing anything!) | */ 00067 /* ---------------------------------------------------- */ 00069 void fmsr_init(void); 00070 00071 00072 /* --------------------------------------------------------------- */ 00073 /* | helper functions (e.g., for memory allocation in application) | */ 00074 /* --------------------------------------------------------------- */ 00077 gf fmsr_nodeid(gf k, gf n, gf index); 00078 00080 gf fmsr_chunks_per_node(gf k, gf n); 00081 00084 gf fmsr_chunks_on_node(gf k, gf n, gf node, gf *chunk_indices); 00085 00087 gf fmsr_nn(gf k, gf n); 00088 00090 gf fmsr_nc(gf k, gf n); 00091 00093 size_t fmsr_padded_size(gf k, gf n, size_t size); 00094 00095 // Examples of how much memory to allocate (WITHOUT error-checking ...) 00096 // Remember, these are just examples. 00097 #define alloc_encode_matrix(k, n) (gf *)malloc(fmsr_nc(k, n) * fmsr_nn(k, n)) 00098 #define alloc_repair_matrix(k, n) (gf *)malloc(fmsr_chunks_per_node(k, n) * (n-1)) 00099 00100 #define alloc_native_chunks(k, n, filesize) \ 00101 (gf *)malloc(fmsr_padded_size(k, n, filesize)) 00102 #define alloc_code_chunks(k, n, filesize) \ 00103 (gf *)malloc(fmsr_nc(k, n) * fmsr_padded_size(k, n, filesize)/fmsr_nn(k, n)) 00104 00105 #define alloc_decode_chunk_indices(k, n) \ 00106 (gf *)malloc(fmsr_nn(k, n)) 00107 #define alloc_decode_chunks(k, n, chunksize) \ 00108 (gf *)malloc(fmsr_nn(k, n) * chunksize) 00109 00110 #define alloc_chunks_to_retrieve(k, n) \ 00111 (gf *)malloc(n-1) 00112 #define alloc_retrieved_chunks(k, n, chunksize) \ 00113 (gf *)malloc((n-1) * chunksize) 00114 #define alloc_new_code_chunks(k, n, chunksize) \ 00115 (gf *)malloc(fmsr_chunks_per_node(k, n) * chunksize) 00116 00117 00118 /* ---------------- */ 00119 /* | core functions | */ 00120 /* ---------------- */ 00121 00136 int fmsr_encode(gf k, gf n, gf *data, size_t data_size, int create_new, 00137 gf *code_chunks, gf *encode_matrix); 00138 00139 00157 int fmsr_decode(gf k, gf n, gf *code_chunks, size_t chunk_size, 00158 gf *chunk_indices, gf num_chunks, gf *encode_matrix, 00159 gf *decode_matrix, int create_new, 00160 gf *data, size_t *data_size); 00161 00162 00179 int fmsr_repair(gf k, gf n, gf *encode_matrix, 00180 gf *erasures, gf num_erasures, fmsr_repair_hints *hints, 00181 gf *new_encode_matrix, gf *repair_matrix, 00182 gf *chunks_to_retrieve, gf *num_chunks_to_retrieve); 00183 00184 00195 void fmsr_regenerate(gf *repair_matrix, gf rows, gf cols, 00196 gf *retrieved_chunks, size_t chunk_size, 00197 gf *new_code_chunks); 00198 00199 00200 #endif /* LIBFMSR_FMSR_H */ 00201