Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sanitise global data allocation; fix a void return #8

Merged
merged 10 commits into from Oct 14, 2017

Conversation

dimpase
Copy link
Contributor

@dimpase dimpase commented Oct 12, 2017

Global data should be allocated in *.c
files, and declared (with extern qualifier) in *.h
Otherwise it is asking for trouble (surely it does not work on OSX).

As well, a non-void function cannot have "return;" - fixed that too.

Global data should be allocated in *.c
files, and declared (with extern qualifier) in *.h
Otherwise it is asking for trouble (surely it does not work on OSX).

As well, a non-void function cannot have "return;" - fixed that too.
@dimpase
Copy link
Contributor Author

dimpase commented Oct 12, 2017

This is a part of the effort to port to OSX, see https://trac.sagemath.org/ticket/24015

@embray
Copy link

embray commented Oct 12, 2017

@dimpase Thanks--this is exactly what I was going to do. I'm not sure how best to deal with fmemopen. It's probably not strictly necessary (it's also not too hard to implement a drop-in replacement but not trivial either).

@dimpase
Copy link
Contributor Author

dimpase commented Oct 12, 2017

I've replaced fmemopen with fopen. There is also an open_memstream to deal with.

@dimpase
Copy link
Contributor Author

dimpase commented Oct 12, 2017

open_memstream is harder to fix, as one has to understand the global scheme of things under the GC used here. Should the string obtained from the stream after it is closed be GC-collectable?

@miguelmarco
Copy link
Owner

Is this branch tested and ready for merge?

@dimpase
Copy link
Contributor Author

dimpase commented Oct 12, 2017

This pull requests appears to work on Linux.

@miguelmarco
Copy link
Owner

I have also run some tests on linux, and nothing seems to be broken. Is this enough to solve the OSX issue or should I wait to make a new release?

@dimpase
Copy link
Contributor Author

dimpase commented Oct 12, 2017 via email

@miguelmarco
Copy link
Owner

Could you give an upper bound on the size of its output, so that we can
just allocate the buffer once and do sprintf to this buffer instead of
fprintf to the (unsupported) stream?

I can't think of a bound that is not overkill on most cases.

What would be the canonical way to append to a string on OSX?

@miguelmarco
Copy link
Owner

miguelmarco commented Oct 12, 2017

One possible solution would be to just remove the p_show function. In fact, the Sage interface doesn't even use it (it uses the homfly_polynomial_dict function which directly extracts the information from the Poly struct).

However, the tests should be adapted in that case.

to do this, we replace fscanfs with sscanfs---
the disadvantage is that we have to manually
advance the place to start to read the string
after each scanf; fortunately we have %n format
parameter to do this easily.
@dimpase
Copy link
Contributor Author

dimpase commented Oct 12, 2017

OK, the latest commit at least makes k_read portable. Let me see about p_show...

replacing fprintf with the sprintf, and manual advancing
of the string position to write to.

here the annoying problem is that we don't have an automatically
growing buffer, so we just allocate 100K---for the time being
@dimpase
Copy link
Contributor Author

dimpase commented Oct 12, 2017

to make this work on OSX, a newer than the current (7.2f) version of Boehm GC is needed;
with 7.2f the test of libhomfly fails, with the master from https://github.com/ivmai/bdwgc/ it does work---at least standalone (it also works within Sage).

now the string to hold the output is grown dynamically,
with a safety region.
@dimpase
Copy link
Contributor Author

dimpase commented Oct 13, 2017

OK, ready, tested on Linux and OSX.

@miguelmarco
Copy link
Owner

Reviewing now.

return FALSE;
}
fscanf(f, "%d ", &links);
sscanf(f, "%d%n", &links, &pos);
f += pos;
for (i=0; i<links; ++i) /* how many pieces of string */
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This kind of updating the position of the f pointer could have side effects. Wouldn't it be safer to use a copy instead of the pointer passed to the function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a call by value. Nothing will happen to f outside the scope of k_read(). (to change f inside k_read(), you'd need to pass &f, i.e. its address).

lib/poly.c Outdated


stream = open_memstream(&bp, &size);
stream = (char *)GC_MALLOC(sizeof(char) * 100000);
bp = stream;
if (!p->len)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you decide the 100000 size limit?

@miguelmarco
Copy link
Owner

Besides my minor concern about the side effects of moving the f pointer (which, according to the tests, don't even seem to have any effect), looks good.

Copy link
Contributor Author

@dimpase dimpase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a C call by value, no need to worry about side effects.

return FALSE;
}
fscanf(f, "%d ", &links);
sscanf(f, "%d%n", &links, &pos);
f += pos;
for (i=0; i<links; ++i) /* how many pieces of string */
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a call by value. Nothing will happen to f outside the scope of k_read(). (to change f inside k_read(), you'd need to pass &f, i.e. its address).

@miguelmarco
Copy link
Owner

Ok. Let me know when it is finally ready, and I will merge it and do a new release.

@dimpase
Copy link
Contributor Author

dimpase commented Oct 14, 2017

it is done in the sense of C code. You can also add the travis stuff. it reports error on OSX as GC 7.6.0 that comes semi-standard is not new enough.

@miguelmarco miguelmarco merged commit c5a3fa2 into miguelmarco:master Oct 14, 2017
@miguelmarco
Copy link
Owner

IS the travis stuff needed for building on OSX?

@miguelmarco
Copy link
Owner

miguelmarco commented Oct 14, 2017

Merged. The tarball is available here

@dimpase
Copy link
Contributor Author

dimpase commented Oct 15, 2017

Could you also do a release on https://github.com/miguelmarco/libhomfly/releases ?

@dimpase
Copy link
Contributor Author

dimpase commented Oct 15, 2017

As you might have already found out, .travis.yml is a configuration script for Travis CI, a continuous integration (i.e. testing) service available for free for github (and not only) repos.
You can sign up for it and do automatic testing of each new commit; you can see
the example output of the repo fork on my account here.

E.g. you can test on OSX even if you don't have OSX on any machine...

@miguelmarco
Copy link
Owner

I created the release; but beware that, since the files generated by autoconf are not tracked by git, the tarball in the github release needs to go through
autoreconf --install before running the usual configure && make flow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants