Browse Source

Start using PAR_ALLOC to make msquares more C++ amenable.

Philip Rideout 10 năm trước cách đây
mục cha
commit
e888d45eb5
5 tập tin đã thay đổi với 74 bổ sung45 xóa
  1. 1 2
      .travis.yml
  2. 2 1
      par_bluenoise.h
  3. 40 39
      par_msquares.h
  4. 15 3
      test/CMakeLists.txt
  5. 16 0
      test/test_cpp.cpp

+ 1 - 2
.travis.yml

@@ -9,7 +9,6 @@ addons:
             - g++-5
             - gcc-5
 script:
-    - clang++ --version
     - export CC=gcc-5 CXX=g++-5
     - cmake -Htest -Bbuild
     - cmake --build build
@@ -17,4 +16,4 @@ script:
     - build/test_bluenoise
     - export CC=clang CXX=clang++
     - cmake -Htest -Bbuild
-    - cmake --build build --clean-first
+    - cmake --build build --clean-first -- VERBOSE=1

+ 2 - 1
par_bluenoise.h

@@ -309,7 +309,8 @@ static par_bluenoise_context* par_bluenoise_create(
         nbytes = (int) ftell(fin);
         fseek(fin, 0, SEEK_SET);
         buf = malloc(nbytes);
-        fread(buf, nbytes, 1, fin);
+        size_t consumed = fread(buf, nbytes, 1, fin);
+        assert(consumed == nbytes);
         fclose(fin);
     }
 

+ 40 - 39
par_msquares.h

@@ -86,9 +86,10 @@ par_msquares_meshlist* par_msquares_grayscale_multi(float const* data,
 #include <assert.h>
 #include <float.h>
 
-#define MIN(a, b) (a > b ? b : a)
-#define MAX(a, b) (a > b ? a : b)
-#define CLAMP(v, lo, hi) MAX(lo, MIN(hi, v))
+#define PAR_MIN(a, b) (a > b ? b : a)
+#define PAR_MAX(a, b) (a > b ? a : b)
+#define PAR_CLAMP(v, lo, hi) PAR_MAX(lo, PAR_MIN(hi, v))
+#define PAR_ALLOC(T, N) ((T*) calloc(N * sizeof(T), 1))
 
 struct par_msquares_meshlist_s {
     int nmeshes;
@@ -103,8 +104,8 @@ static par_msquares_meshlist* par_msquares_merge(par_msquares_meshlist** lists,
 
 static void par_init_tables()
 {
-    point_table = malloc(16 * sizeof(int*));
-    triangle_table = malloc(16 * sizeof(int*));
+    point_table = PAR_ALLOC(int*, 16);
+    triangle_table = PAR_ALLOC(int*, 16);
 
     char const* CODE_TABLE =
         "0 0\n"
@@ -131,11 +132,10 @@ static void par_init_tables()
         table_token += 2;
         int ntris = *table_token - '0';
         table_token += 2;
-        int* sqrtris = triangle_table[i] =
-                malloc((ntris + 1) * 3 * sizeof(int));
+        int* sqrtris = triangle_table[i] = PAR_ALLOC(int, (ntris + 1) * 3);
         sqrtris[0] = ntris;
         int mask = 0;
-        int* sqrpts = point_table[i] = malloc(7 * sizeof(int));
+        int* sqrpts = point_table[i] = PAR_ALLOC(int, 7);
         sqrpts[0] = 0;
         for (int j = 0; j < ntris * 3; j++, table_token += 2) {
             int midp = *table_token - '0';
@@ -176,8 +176,8 @@ static int gray_multi_inside(int location, void* contextptr)
 static float gray_height(float x, float y, void* contextptr)
 {
     gray_context* context = (gray_context*) contextptr;
-    int i = CLAMP(context->width * x, 0, context->width - 1);
-    int j = CLAMP(context->height * y, 0, context->height - 1);
+    int i = PAR_CLAMP(context->width * x, 0, context->width - 1);
+    int j = PAR_CLAMP(context->height * y, 0, context->height - 1);
     return context->data[i + j * context->width];
 }
 
@@ -205,8 +205,8 @@ static float color_height(float x, float y, void* contextptr)
 {
     color_context* context = (color_context*) contextptr;
     assert(context->bpp == 4);
-    int i = CLAMP(context->width * x, 0, context->width - 1);
-    int j = CLAMP(context->height * y, 0, context->height - 1);
+    int i = PAR_CLAMP(context->width * x, 0, context->width - 1);
+    int j = PAR_CLAMP(context->height * y, 0, context->height - 1);
     int k = i + j * context->width;
     return context->data[k * 4 + 3] / 255.0;
 }
@@ -244,7 +244,7 @@ par_msquares_meshlist* par_msquares_grayscale_multi(float const* data,
     int nthresholds, int flags)
 {
     par_msquares_meshlist* mlists[2];
-    mlists[0] = calloc(sizeof(par_msquares_meshlist), 1);
+    mlists[0] = PAR_ALLOC(par_msquares_meshlist, 1);
     int connect = flags & PAR_MSQUARES_CONNECT;
     int snap = flags & PAR_MSQUARES_SNAP;
     int heights = flags & PAR_MSQUARES_HEIGHTS;
@@ -311,12 +311,12 @@ void par_msquares_free(par_msquares_meshlist* mlist)
 static par_msquares_meshlist* par_msquares_merge(par_msquares_meshlist** lists,
     int count, int snap)
 {
-    par_msquares_meshlist* merged = malloc(sizeof(par_msquares_meshlist));
+    par_msquares_meshlist* merged = PAR_ALLOC(par_msquares_meshlist, 1);
     merged->nmeshes = 0;
     for (int i = 0; i < count; i++) {
         merged->nmeshes += lists[i]->nmeshes;
     }
-    merged->meshes = malloc(sizeof(par_msquares_mesh*) * merged->nmeshes);
+    merged->meshes = PAR_ALLOC(par_msquares_mesh*, merged->nmeshes);
     par_msquares_mesh** pmesh = merged->meshes;
     for (int i = 0; i < count; i++) {
         par_msquares_meshlist* meshlist = lists[i];
@@ -334,8 +334,8 @@ static par_msquares_meshlist* par_msquares_merge(par_msquares_meshlist** lists,
     for (int i = 0; i < merged->nmeshes; i++, pmesh++) {
         float* pzed = (*pmesh)->points + 2;
         for (int j = 0; j < (*pmesh)->npoints; j++, pzed += 3) {
-            zmin = MIN(*pzed, zmin);
-            zmax = MAX(*pzed, zmax);
+            zmin = PAR_MIN(*pzed, zmin);
+            zmax = PAR_MAX(*pzed, zmax);
         }
     }
     float zextent = zmax - zmin;
@@ -357,7 +357,7 @@ static par_msquares_meshlist* par_msquares_merge(par_msquares_meshlist** lists,
         // tessellation code, which generates two "connector" triangles for each
         // extruded edge.  The first two verts of the second triangle are the
         // verts that need to be displaced.
-        char* markers = calloc(mesh->npoints, 1);
+        char* markers = PAR_ALLOC(char, mesh->npoints);
         int tri = mesh->ntriangles - mesh->nconntriangles;
         while (tri < mesh->ntriangles) {
             markers[mesh->triangles[tri * 3 + 3]] = 1;
@@ -419,10 +419,10 @@ par_msquares_meshlist* par_msquares_function(int width, int height,
 
     // Allocate the meshlist and the first mesh.
 
-    par_msquares_meshlist* mlist = malloc(sizeof(par_msquares_meshlist));
+    par_msquares_meshlist* mlist = PAR_ALLOC(par_msquares_meshlist, 1);
     mlist->nmeshes = 1;
-    mlist->meshes = malloc(sizeof(par_msquares_mesh*));
-    mlist->meshes[0] = malloc(sizeof(par_msquares_mesh));
+    mlist->meshes = PAR_ALLOC(par_msquares_mesh*, 1);
+    mlist->meshes[0] = PAR_ALLOC(par_msquares_mesh, 1);
     par_msquares_mesh* mesh = mlist->meshes[0];
     mesh->dim = (flags & PAR_MSQUARES_HEIGHTS) ? 3 : 2;
     int ncols = width / cellsize;
@@ -442,18 +442,18 @@ par_msquares_meshlist* par_msquares_function(int width, int height,
     int nconntris = 0;
     uint16_t* edgemap = 0;
     if (flags & PAR_MSQUARES_CONNECT) {
-        conntris = malloc(maxedges * 6 * sizeof(uint16_t));
+        conntris = PAR_ALLOC(uint16_t, maxedges * 6);
         maxtris +=  maxedges * 2;
         maxpts += maxedges * 2;
-        edgemap = malloc(maxpts * sizeof(uint16_t));
+        edgemap = PAR_ALLOC(uint16_t, maxpts);
         for (int i = 0; i < maxpts; i++) {
             edgemap[i] = 0xffff;
         }
     }
 
-    uint16_t* tris = malloc(maxtris * 3 * sizeof(uint16_t));
+    uint16_t* tris = PAR_ALLOC(uint16_t, maxtris * 3);
     int ntris = 0;
-    float* pts = malloc(maxpts * mesh->dim * sizeof(float));
+    float* pts = PAR_ALLOC(float, maxpts * mesh->dim);
     int npts = 0;
 
     // The "verts" x/y/z arrays are the 4 corners and 4 midpoints around the
@@ -461,15 +461,15 @@ par_msquares_meshlist* par_msquares_function(int width, int height,
     // the lower-left, although we expect the image data to be in raster order
     // (starts at top-left).
 
-    float normalization = 1.0f / MAX(width, height);
+    float normalization = 1.0f / PAR_MAX(width, height);
     float normalized_cellsize = cellsize * normalization;
     int maxrow = (height - 1) * width;
     uint16_t* ptris = tris;
     uint16_t* pconntris = conntris;
     float* ppts = pts;
     float vertsx[8], vertsy[8];
-    uint8_t* prevrowmasks = calloc(ncols, 1);
-    int* prevrowinds = calloc(sizeof(int) * ncols * 3, 1);
+    uint8_t* prevrowmasks = PAR_ALLOC(uint8_t, ncols);
+    int* prevrowinds = PAR_ALLOC(int, ncols * 3);
 
     // If simplification is enabled, we need to track all 'F' cells and their
     // repsective triangle indices.
@@ -477,9 +477,9 @@ par_msquares_meshlist* par_msquares_function(int width, int height,
     uint16_t* simplification_tris = 0;
     uint8_t* simplification_ntris = 0;
     if (flags & PAR_MSQUARES_SIMPLIFY) {
-        simplification_codes = malloc(nrows * ncols);
-        simplification_tris = malloc(nrows * ncols * sizeof(uint16_t));
-        simplification_ntris = malloc(nrows * ncols);
+        simplification_codes = PAR_ALLOC(uint8_t, nrows * ncols);
+        simplification_tris = PAR_ALLOC(uint16_t, nrows * ncols);
+        simplification_ntris = PAR_ALLOC(uint8_t, nrows * ncols);
     }
 
     // Do the march!
@@ -492,7 +492,7 @@ par_msquares_meshlist* par_msquares_function(int width, int height,
         vertsy[3] = vertsy[7] = normalized_cellsize * (row + 0.5);
 
         int northi = row * cellsize * width;
-        int southi = MIN(northi + cellsize * width, maxrow);
+        int southi = PAR_MIN(northi + cellsize * width, maxrow);
         int northwest = invert ^ insidefn(northi, context);
         int southwest = invert ^ insidefn(southi, context);
         int previnds[8] = {0};
@@ -759,7 +759,7 @@ par_msquares_meshlist* par_msquares_function(int width, int height,
         // Build a new index array cell-by-cell.  If any given cell is 'F' and
         // its neighbor to the south is also 'F', then it's part of a run.
         int nnewtris = ntris + nconntris - neliminated_triangles;
-        uint16_t* newtris = malloc(nnewtris * 3 * sizeof(uint16_t));
+        uint16_t* newtris = PAR_ALLOC(uint16_t, nnewtris * 3);
         uint16_t* pnewtris = newtris;
         in_run = 0;
         for (int row = 0; row < nrows - 1; row += 2) {
@@ -842,7 +842,7 @@ par_msquares_meshlist* par_msquares_function(int width, int height,
         free(simplification_ntris);
 
         // Remove unreferenced points.
-        char* markers = calloc(npts, 1);
+        char* markers = PAR_ALLOC(char, npts);
         ptris = tris;
         int newnpts = 0;
         for (int i = 0; i < ntris * 3; i++, ptris++) {
@@ -857,8 +857,8 @@ par_msquares_meshlist* par_msquares_function(int width, int height,
                 markers[conntris[i]] = 1;
             }
         }
-        float* newpts = malloc(newnpts * mesh->dim * sizeof(float));
-        uint16_t* mapping = malloc((ntris + nconntris) * 3 * sizeof(uint16_t));
+        float* newpts = PAR_ALLOC(float, newnpts * mesh->dim);
+        uint16_t* mapping = PAR_ALLOC(uint16_t, (ntris + nconntris) * 3);
         ppts = pts;
         float* pnewpts = newpts;
         int j = 0;
@@ -915,7 +915,8 @@ par_msquares_meshlist* par_msquares_function(int width, int height,
     return mlist;
 }
 
-#undef MIN
-#undef MAX
-#undef CLAMP
+#undef PAR_MIN
+#undef PAR_MAX
+#undef PAR_CLAMP
+#undef PAR_ALLOC
 #endif

+ 15 - 3
test/CMakeLists.txt

@@ -3,8 +3,8 @@ project(par)
 find_package(PkgConfig REQUIRED)
 pkg_search_module(CURL REQUIRED libcurl)
 
-set(CMAKE_C_FLAGS "-std=c11 -O3 -Wall")
-set(CMAKE_CXX_FLAGS "-O3 -Wall")
+set(CMAKE_C_FLAGS "-std=c11 -O3 -Wall -Werror")
+set(CMAKE_CXX_FLAGS "-O3 -Wall -Werror")
 
 include_directories(
     .
@@ -27,10 +27,22 @@ add_executable(
     sds.c
     whereami.c)
 
+add_executable(
+    test_cpp
+    test_cpp.cpp
+    lz4.cpp
+    lodepng.c
+    sds.c
+    whereami.c)
+
 target_link_libraries(
     test_msquares
     ${CURL_LIBRARIES})
 
 target_link_libraries(
     test_bluenoise
-    ${CURL_LIBRARIES})
+    ${CURL_LIBRARIES})
+
+target_link_libraries(
+    test_cpp
+    ${CURL_LIBRARIES})

+ 16 - 0
test/test_cpp.cpp

@@ -0,0 +1,16 @@
+#define PAR_MSQUARES_IMPLEMENTATION
+#include "par_msquares.h"
+
+// #define PAR_BLUENOISE_IMPLEMENTATION
+// #include "par_bluenoise.h"
+//
+// #define PAR_EASYCURL_IMPLEMENTATION
+// #include "par_easycurl.h"
+//
+// #define PAR_FILECACHE_IMPLEMENTATION
+// #include "par_filecache.h"
+
+int main(int argc, char* argv[])
+{
+    return 0;
+}