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

use malloc/free for WiFiManagerParameter* array instead of hard coding. #374

Merged
merged 8 commits into from
Jan 30, 2018

Conversation

liebman
Copy link
Contributor

@liebman liebman commented Jun 4, 2017

Dynamically allocate the _params array. Existing no arg constructor uses the same default, 10, as it used to. Added a second constructor that allows setting size. Added Destructor that frees allocated _params.

add alternate constructor to specifiy max parameter count
origional default constructor uses old hard codded value
@merlokk
Copy link

merlokk commented Jun 26, 2017

WiFiManager(int max_params = 10);

and return to one constructor

@tablatronix
Copy link
Collaborator

hmm not sure how I feel about the constructor params

@liebman
Copy link
Contributor Author

liebman commented Aug 31, 2017

Is there a case where constructor params don't work? Semantically this is no different than multiple constructors and reduces the code complexity.

@tablatronix
Copy link
Collaborator

I dont like passing params via constructors it causes limitation in the future with regards to backward compatibility

@liebman
Copy link
Contributor Author

liebman commented Sep 1, 2017

I don't think I understand. Are you saying that there should be no constructors that have parameters?

@tablatronix
Copy link
Collaborator

tablatronix commented Sep 2, 2017

not really, just that variable arguments should not be passed via a constructor, a constructor should be passed data that an object needs to work, not a variable parameter that can also be set via setters etc. It present maintenance problems in the future. For example what happens when we refactor the parameter system, or abstract it out into a separate dependency then we have this legacy constructor definition that cannot be reused, and has to be deprecated or handled.

What happens if we need to add more parameters to constructor , what if they are the same datatype ? Its can become a mess.

If we were to say allow a config object to preload parameters, then that would be acceptable.
eg. constructor(object[arg1:blargh,arg2:blargh]){_localarg1 = checkvaliditiy(object:arg1) }

For this i would have the constructor call a default param init, then require a explicit init from the user to reallocate the param size etc. yes its a pita for the user, but it is clear what the code is doing, and they are overriding! default values in the class

… the parameter array each time the size limit is reached.

I used the initial size (WIFI_MANAGER_MAX_PARAMS) as the increment when resizing.
Existing data is coppied to the new array and the old one is free'd.
@liebman
Copy link
Contributor Author

liebman commented Oct 26, 2017

Ok. I re-worked the change so that it uses the default constructor and resizes the params array each time the size is exceeded. Better?

@tablatronix
Copy link
Collaborator

Ooh thats a great idea, sorry about this ainwas going to merge and fixup but I have been pre occupied

@liebman
Copy link
Contributor Author

liebman commented Oct 27, 2017

Made the change simpler too.

@tablatronix
Copy link
Collaborator

tablatronix commented Nov 16, 2017

I assume you have tested this a bit ?
Do we need to clamp this at all to make sure there is a max max, memorywise?

@liebman
Copy link
Contributor Author

liebman commented Nov 16, 2017

I've been using it in a few projects constantly since the commits. I don't think we should add limits, if the user wants to add 10,000 params why should we stop them? ;-)

@tablatronix
Copy link
Collaborator

Aside from malloc exceptions im sure they will figure it out. Thanks I will merge this then bring it into my dev branch

WiFiManager.cpp Outdated
WiFiManagerParameter** new_params = (WiFiManagerParameter**)malloc(_max_params * sizeof(WiFiManagerParameter*));
// copy old data
memcpy(new_params, _params, _paramsCount * sizeof(WiFiManagerParameter*));
free(_params);
Copy link

Choose a reason for hiding this comment

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

Doesn't this malloc->memcpy->free lead to fragmented heap?
As i understand adding 30 items means i will have in heap 10 + 20 unusable free space followed by 30 for _params?
Using realloc may reduce chance of fragmented heap.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very good point. I'll update the PR

… returns bool

indicating the status of adding the parameter where false indicates it failed to
increase the size of _params and did not add the parameter.
@tablatronix tablatronix merged commit a4795b7 into tzapu:master Jan 30, 2018
@liebman liebman deleted the allocate-params-array branch January 30, 2018 18:42
@tablatronix
Copy link
Collaborator

tablatronix commented Jan 31, 2018

Changed to == maxparams, and changed max params to 5
5f78d38

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

Successfully merging this pull request may close these issues.

4 participants