Skip to content

Commit 70cbef3

Browse files
author
Weijia Wang
committed
Port to Windows
1 parent c1890f8 commit 70cbef3

File tree

10 files changed

+304
-118
lines changed

10 files changed

+304
-118
lines changed

src/fglm/aligned_alloc.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* This file is part of msolve.
2+
*
3+
* msolve is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 2 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* msolve is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with msolve. If not, see <https://www.gnu.org/licenses/>
15+
*
16+
* Authors:
17+
* Jérémy Berthomieu
18+
* Christian Eder
19+
* Mohab Safey El Din */
20+
21+
#ifndef ALIGNED_ALLOC_HEADER_H
22+
#define ALIGNED_ALLOC_HEADER_H
23+
24+
#ifdef _WIN32
25+
26+
#include <errno.h>
27+
#include <malloc.h>
28+
29+
static inline int posix_memalign(void **__memptr, size_t __alignment, size_t __size)
30+
{
31+
void *p = _aligned_malloc(__size, __alignment);
32+
if (!p)
33+
{
34+
return ENOMEM;
35+
}
36+
*__memptr = p;
37+
return 0;
38+
}
39+
#endif
40+
41+
static inline void posix_memalign_free(void *__p)
42+
{
43+
#ifdef _WIN32
44+
_aligned_free(__p);
45+
#else
46+
free(__p);
47+
#endif
48+
}
49+
50+
#endif /* ALIGNED_ALLOC_HEADER_H */

src/fglm/data_fglm.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <flint/nmod_poly_factor.h>
2727
#include <flint/ulong_extras.h>
2828

29+
#include "aligned_alloc.h"
2930

3031
static inline void free_sp_mat_fglm(sp_matfglm_t *mat){
3132
if(mat!=NULL){
@@ -80,10 +81,10 @@ static inline fglm_data_t *allocate_fglm_data(szmat_t nrows, szmat_t ncols, szma
8081

8182

8283
static inline void free_fglm_data(fglm_data_t *data){
83-
free(data->vecinit);
84-
free(data->res);
85-
free(data->vecmult);
86-
free(data->vvec);
84+
posix_memalign_free(data->vecinit);
85+
posix_memalign_free(data->res);
86+
posix_memalign_free(data->vecmult);
87+
posix_memalign_free(data->vvec);
8788
free(data->pts);
8889
free(data);
8990
}

src/fglm/fglm_core.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ double omp_get_wtime(void) { return realtime();}
7070
#include "../upolmat/nmod_poly_mat_pmbasis.c"
7171
#endif
7272

73+
#include "aligned_alloc.h"
74+
7375
void print_fglm_data(
7476
FILE *file,
7577
const md_t * const st,
@@ -756,9 +758,9 @@ static void generate_matrix_sequence(sp_matfglm_t *matxn, fglm_data_t *data,
756758
RED_32,
757759
RED_64);
758760
}
759-
free(Rmat);
760-
free(res);
761-
free(tres);
761+
posix_memalign_free(Rmat);
762+
posix_memalign_free(res);
763+
posix_memalign_free(tres);
762764

763765
}
764766

@@ -1525,7 +1527,7 @@ param_t *nmod_fglm_compute_trace_data(sp_matfglm_t *matrix, mod_t prime,
15251527
#endif
15261528

15271529
#if DEBUGFGLM >= 1
1528-
FILE *fmat = fopen("/tmp/matrix.fglm", "w");
1530+
FILE *fmat = fopen("/tmp/matrix.fglm", "wb");
15291531
display_fglm_matrix(fmat, matrix);
15301532
fclose(fmat);
15311533
#endif
@@ -1728,7 +1730,7 @@ int nmod_fglm_compute_apply_trace_data(sp_matfglm_t *matrix,
17281730
#endif
17291731

17301732
#if DEBUGFGLM >= 1
1731-
FILE *fmat = fopen("/tmp/matrix.fglm", "w");
1733+
FILE *fmat = fopen("/tmp/matrix.fglm", "wb");
17321734
display_fglm_matrix(fmat, matrix);
17331735
fclose(fmat);
17341736
#endif
@@ -2077,7 +2079,7 @@ param_t *nmod_fglm_guess_colon(sp_matfglmcol_t *matrix,
20772079
#endif
20782080

20792081
#if DEBUGFGLM >= 1
2080-
FILE *fmat = fopen("/tmp/matrix.fglm", "w");
2082+
FILE *fmat = fopen("/tmp/matrix.fglm", "wb");
20812083
display_fglm_colon_matrix(fmat, matrix);
20822084
fclose(fmat);
20832085
#endif

src/msolve/getdelim.h

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/* getdelim.h --- Implementation of replacement getdelim/getline function.
2+
Copyright (C) 1994, 1996-1998, 2001, 2003, 2005-2025 Free Software
3+
Foundation, Inc.
4+
5+
This file is free software: you can redistribute it and/or modify
6+
it under the terms of the GNU Lesser General Public License as
7+
published by the Free Software Foundation; either version 2.1 of the
8+
License, or (at your option) any later version.
9+
10+
This file is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
GNU Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public License
16+
along with this program. If not, see <https://www.gnu.org/licenses/>. */
17+
18+
/* Ported from glibc by Simon Josefsson. */
19+
20+
#ifndef GETDELIM_HEADER_H
21+
#define GETDELIM_HEADER_H
22+
23+
#ifdef _WIN32
24+
25+
#include <stdio.h>
26+
27+
#include <limits.h>
28+
#include <stdint.h>
29+
#include <stdlib.h>
30+
#include <errno.h>
31+
32+
static inline void
33+
alloc_failed (void)
34+
{
35+
#if defined _WIN32 && ! defined __CYGWIN__
36+
/* Avoid errno problem without using the realloc module; see:
37+
https://lists.gnu.org/r/bug-gnulib/2016-08/msg00025.html */
38+
errno = ENOMEM;
39+
#endif
40+
}
41+
42+
/* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
43+
NUL-terminate it). *LINEPTR is a pointer returned from malloc (or
44+
NULL), pointing to *N characters of space. It is realloc'ed as
45+
necessary. Returns the number of characters read (not including
46+
the null terminator), or -1 on error or EOF. */
47+
48+
static inline ssize_t
49+
getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
50+
{
51+
ssize_t result;
52+
size_t cur_len = 0;
53+
54+
if (lineptr == NULL || n == NULL || fp == NULL)
55+
{
56+
errno = EINVAL;
57+
return -1;
58+
}
59+
60+
if (*lineptr == NULL || *n == 0)
61+
{
62+
char *new_lineptr;
63+
*n = 120;
64+
new_lineptr = (char *) realloc (*lineptr, *n);
65+
if (new_lineptr == NULL)
66+
{
67+
alloc_failed ();
68+
return -1;
69+
}
70+
*lineptr = new_lineptr;
71+
}
72+
73+
for (;;)
74+
{
75+
int i;
76+
77+
i = getc (fp);
78+
if (i == EOF)
79+
{
80+
result = -1;
81+
break;
82+
}
83+
84+
/* Make enough space for len+1 (for final NUL) bytes. */
85+
if (cur_len + 1 >= *n)
86+
{
87+
size_t needed_max =
88+
SSIZE_MAX < SIZE_MAX ? (size_t) SSIZE_MAX + 1 : SIZE_MAX;
89+
size_t needed = 2 * *n + 1; /* Be generous. */
90+
char *new_lineptr;
91+
92+
if (needed_max < needed)
93+
needed = needed_max;
94+
if (cur_len + 1 >= needed)
95+
{
96+
errno = EOVERFLOW;
97+
return -1;
98+
}
99+
100+
new_lineptr = (char *) realloc (*lineptr, needed);
101+
if (new_lineptr == NULL)
102+
{
103+
alloc_failed ();
104+
return -1;
105+
}
106+
107+
*lineptr = new_lineptr;
108+
*n = needed;
109+
}
110+
111+
(*lineptr)[cur_len] = i;
112+
cur_len++;
113+
114+
if (i == delimiter)
115+
break;
116+
}
117+
(*lineptr)[cur_len] = '\0';
118+
result = cur_len ? cur_len : result;
119+
120+
return result;
121+
}
122+
123+
static inline ssize_t
124+
getline (char **lineptr, size_t *n, FILE *stream)
125+
{
126+
return getdelim (lineptr, n, '\n', stream);
127+
}
128+
129+
#endif
130+
131+
#endif /* GETDELIM_HEADER_H */

0 commit comments

Comments
 (0)