Skip to content

Commit 4375f75

Browse files
authored
Merge pull request #2 from swig-fortran/ownership
Improve memory ownership mechanics
2 parents c467b17 + 777c4cb commit 4375f75

24 files changed

+464
-242
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ matrix:
1919
packages:
2020
- gfortran
2121
- python3-sphinx
22+
- valgrind
2223
- os: linux
2324
env: FLIBCPP_DEV=OFF GENERATOR=make
2425
FLIBCPP_FORTRAN_STD=f2008

CMakeLists.txt

+11-7
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@ project(Flibcpp VERSION 0.2.1 LANGUAGES CXX Fortran)
1212
# OPTIONS
1313
#---------------------------------------------------------------------------#
1414

15+
if (FLIBCPP_DEV OR FLIBCPP_BUILD_EXAMPLES OR FLIBCPP_BUILD_TESTS)
16+
set(_DEFAULT_BUILD_TESTING ON)
17+
endif()
18+
1519
option(BUILD_SHARED_LIBS "Build shared libraries" ON)
20+
option(BUILD_TESTING "Enable CTest" ${_DEFAULT_BUILD_TESTING})
1621
option(FLIBCPP_DEV "Default to using development-centered options" OFF)
1722
option(FLIBCPP_BUILD_DOCS "Build documentation with Sphinx" ${FLIBCPP_DEV})
1823
option(FLIBCPP_BUILD_EXAMPLES "Build examples" ON)
19-
option(FLIBCPP_BUILD_TESTS "Build unit tests" ${FLIBCPP_DEV})
24+
option(FLIBCPP_BUILD_TESTS "Build Flibcpp tests" ${BUILD_TESTING})
2025
option(FLIBCPP_USE_SWIG "Regenerate source files using SWIG" ${FLIBCPP_DEV})
2126

2227
#---------------------------------------------------------------------------#
@@ -37,7 +42,7 @@ set(FLIBCPP_FORTRAN_STD "f2003" CACHE STRING
3742
"Fortran standard for compiling generated code")
3843

3944
#---------------------------------------------------------------------------#
40-
# SWIG setup
45+
# MODULES TO LOAD
4146
#---------------------------------------------------------------------------#
4247

4348
if (FLIBCPP_USE_SWIG)
@@ -60,6 +65,9 @@ else()
6065
set(FLIBCPP_USE_SWIG FALSE)
6166
endif()
6267

68+
# Enable testing based on BUILD_TESTING flag
69+
include(CTest)
70+
6371
#---------------------------------------------------------------------------#
6472
# VERSIONING
6573
#---------------------------------------------------------------------------#
@@ -72,7 +80,7 @@ file(STRINGS "${CMAKE_CURRENT_SOURCE_DIR}/cmake/git-version.txt"
7280
if (NOT FLIBCPP_VERSION_STRING MATCHES "\\$Format:")
7381
# First line are decorators, second is hash
7482
list(GET FLIBCPP_VERSION_STRING 0 _tag)
75-
string(REGEX REPLACE ".*, *(tag: *)?" "" _tag "${_tag}")
83+
string(REGEX REPLACE "tag: *" "" _tag "${_tag}")
7684
list(GET FLIBCPP_VERSION_STRING 1 _hash)
7785
string(REGEX REPLACE " +" "" _hash "${_hash}")
7886
set(FLIBCPP_VERSION_STRING "${_tag}-g${_hash}")
@@ -249,10 +257,6 @@ install(FILES
249257
# TESTING AND DOCS
250258
#---------------------------------------------------------------------------#
251259

252-
if (FLIBCPP_BUILD_TESTS OR FLIBCPP_BUILD_EXAMPLES)
253-
enable_testing()
254-
endif()
255-
256260
if (FLIBCPP_BUILD_TESTS)
257261
add_subdirectory(test)
258262
endif()

example/CMakeLists.txt

+6-4
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ swig_fortran_add_example(sort
1717
swig_fortran_add_example(vecstr
1818
flc_string flc_vector)
1919

20-
add_test(
21-
NAME examples
22-
COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/run-examples.sh"
23-
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
20+
if (BUILD_TESTING)
21+
add_test(
22+
NAME examples
23+
COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/run-examples.sh"
24+
WORKING_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
25+
endif()
2426

2527
##---------------------------------------------------------------------------##
2628
## end of common/CMakeLists.txt

example/sort.f90

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ program sort_example
3838

3939
! Write output
4040
write(STDOUT, "(a, 4(f8.3,','))") "First few elements:", x(:min(4, size(x)))
41+
42+
call rng%release()
43+
44+
! Valgrind fails without deallocating the array, but technically it's not
45+
! necessary in Fortran to do this
46+
deallocate(x)
4147
contains
4248

4349
! Loop until the user inputs a positive integer. Catch error conditions.

example/vecstr.f90

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ program vecstr_example
2727

2828
if (vec%empty()) then
2929
write(STDOUT, *) "No vectors provided"
30+
call vec%release()
3031
stop 0
3132
endif
3233

include/flc.i

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
%module "flc"
99

10+
#if defined(SWIGIMPORTED) && !defined(SWIGIMPORTED)
11+
#error "To import the FLC module correctly, use ``%include \"import_flc.i\"``"
12+
#endif
13+
1014
%define %flc_add_header
1115
%insert("fbegin") %{
1216
! Flibcpp project, https://github.com/swig-fortran/flibcpp
@@ -79,7 +83,7 @@ using std::size_t;
7983
* Linked into auto-generated file flibcpp_version.cpp
8084
************************/
8185

82-
%apply const char* { const char flibcpp_version[] };
86+
%apply char* { const char flibcpp_version[] };
8387
%fortranbindc flibcpp_version_major;
8488
%fortranbindc flibcpp_version_minor;
8589
%fortranbindc flibcpp_version_patch;

include/flc_random.i

+4-3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#endif
2828

2929
%rename(Engine) std::SWIG_MERSENNE_TWISTER;
30+
%fortran_autofree_rvalue(std::SWIG_MERSENNE_TWISTER);
3031

3132
namespace std {
3233
class SWIG_MERSENNE_TWISTER
@@ -35,9 +36,9 @@ class SWIG_MERSENNE_TWISTER
3536
typedef SWIG_MERSENNE_RESULT_TYPE result_type;
3637

3738
SWIG_MERSENNE_TWISTER();
38-
explicit SWIG_MERSENNE_TWISTER(result_type);
39-
void seed(result_type);
40-
void discard(unsigned long long);
39+
explicit SWIG_MERSENNE_TWISTER(result_type seed_value);
40+
void seed(result_type seed_value);
41+
void discard(unsigned long long count);
4142
};
4243
} // namespace std
4344

include/flc_string.i

+3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
$result = ($1 == std::string::npos ? 0 : $1 + 1);
2323
}
2424

25+
// Automatically free temporary strings as appropriate
26+
%fortran_autofree_rvalue(std::string);
27+
2528
namespace std {
2629
class string {
2730
public:

include/flc_vector.i

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
%define %specialize_std_vector_pod(T)
5252
namespace std {
5353
template<> class vector<T> {
54+
// Automatically free temporary vectors as appropriate
55+
%fortran_autofree_rvalue(std::vector<T>);
56+
5457
SWIG_STD_VECTOR_COMMON(T, const T&)
5558
SWIG_STD_VECTOR_REF(T)
5659
%extend {
@@ -72,6 +75,7 @@ namespace std {
7275

7376
/* ------------------------------------------------------------------------- */
7477

78+
%fortran_autofree_rvalue(std::vector<std::string>);
7579
%extend std::vector<std::string> {
7680
void set_ref(size_type index, std::string& str) {
7781
SWIG_check_range(index, $self->size(),

include/import_flc.i

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
* Distributed under an MIT open source license: see LICENSE for details.
66
*/
77

8-
#ifdef SWIGIMPORTED
9-
#error "To use this header, %include "import_flc.i", don't %import it"
8+
// Make sure that the downstream module isn't doing ``%import "import_flc.i"``
9+
// or ``%import "flc.i"``: only ``%include "import_flc.i"``
10+
#ifndef SWIGIMPORTED
11+
#define FLC_SWIGIMPORTED
1012
#endif
1113

1214
// Set up macros, etc.

scripts/travis/test.sh

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
# File : scripts/travis/test.sh
44
###############################################################################
55

6-
cd ${BUILD_ROOT} && ctest --output-on-failure
6+
cd ${BUILD_ROOT}
7+
8+
ctest --output-on-failure
9+
if [ "${FLIBCPP_DEV}" = "ON" ]; then
10+
ctest -E examples -D ExperimentalMemCheck --output-on-failure
11+
fi
712

813
###############################################################################
914
# end of scripts/travis/test.sh

src/flc.f90

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function swigc_flibcpp_version_get() &
7171
contains
7272
! MODULE SUBPROGRAMS
7373

74-
subroutine SWIGTM_fout_const_SS_char_Sm_(imout, fout)
74+
subroutine SWIGTM_fout_char_Sm_(imout, fout)
7575
use, intrinsic :: ISO_C_BINDING
7676
type(SwigArrayWrapper), intent(in) :: imout
7777
character(kind=C_CHAR, len=:), allocatable, intent(out) :: fout
@@ -91,7 +91,7 @@ function get_serr() &
9191
type(SwigArrayWrapper) :: fresult
9292

9393
fresult = swigc_get_serr()
94-
call SWIGTM_fout_const_SS_char_Sm_(fresult, swig_result)
94+
call SWIGTM_fout_char_Sm_(fresult, swig_result)
9595
if (.false.) call SWIG_free(fresult%data)
9696
end function
9797

@@ -102,7 +102,7 @@ function get_flibcpp_version() &
102102
type(SwigArrayWrapper) :: fresult
103103

104104
fresult = swigc_flibcpp_version_get()
105-
call SWIGTM_fout_const_SS_char_Sm_(fresult, swig_result)
105+
call SWIGTM_fout_char_Sm_(fresult, swig_result)
106106
if (.false.) call SWIG_free(fresult%data)
107107
end function
108108

src/flcFORTRAN_wrap.cxx

+2-2
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ SWIGEXPORT SwigArrayWrapper _wrap_get_serr() {
345345
char *result = 0 ;
346346

347347
result = (char *)flc_get_serr();
348-
fresult.size = strlen(reinterpret_cast< const char* >(result));
348+
fresult.size = strlen((char*)(result));
349349
fresult.data = const_cast< char * >(result);
350350
return fresult;
351351
}
@@ -356,7 +356,7 @@ SWIGEXPORT SwigArrayWrapper _wrap_flibcpp_version_get() {
356356
char *result = 0 ;
357357

358358
result = (char *)(char *)flibcpp_version;
359-
fresult.size = strlen(reinterpret_cast< const char* >(result));
359+
fresult.size = strlen((char*)(result));
360360
fresult.data = const_cast< char * >(result);
361361
return fresult;
362362
}

0 commit comments

Comments
 (0)