Skip to content

Conversation

@casperdcl
Copy link

@casperdcl casperdcl commented Jan 21, 2021

Fixes a pretty major malloc error in the docs.

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

Recognized GitHub username

We couldn't find a bugs.python.org (b.p.o) account corresponding to the following GitHub usernames:

@casperdcl

This might be simply due to a missing "GitHub Name" entry in one's b.p.o account settings. This is necessary for legal reasons before we can look at this contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Feb 24, 2021
@casperdcl
Copy link
Author

Ping

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Feb 25, 2021
@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Mar 28, 2021
@casperdcl
Copy link
Author

@vstinner @serhiy-storchaka maybe you could take a look?

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Mar 29, 2021
@vstinner
Copy link
Member

Fixes a pretty major malloc error in the docs.

Please elaborate. I don't understand which problem you are trying to solve.

@casperdcl
Copy link
Author

The CPython API docs currently effectively suggest malloc(0 * sizeof(T)) when it should be malloc(1 * sizeof(T)) to create a new object. This PR fixes this.

@vstinner
Copy link
Member

The CPython API docs currently effectively suggest malloc(0 * sizeof(T)) when it should be malloc(1 * sizeof(T)) to create a new object. This PR fixes this.

tp_alloc(type, 0) doesn't call malloc().

The default allocator function is PyType_GenericAlloc() which uses:

    const size_t size = _PyObject_VAR_SIZE(type, nitems+1);

which is defined as:

#define _PyObject_VAR_SIZE(typeobj, nitems)     \
    _Py_SIZE_ROUND_UP((typeobj)->tp_basicsize + \
        (nitems)*(typeobj)->tp_itemsize,        \
        SIZEOF_VOID_P)

tp_alloc(type, 0) allocates types->tp_basicsize. In the tutorial, tp_basicsize is defined as:

.tp_basicsize = sizeof(CustomObject),

In short, IMO the current code is correct, and your change wastes memory.

@casperdcl
Copy link
Author

Hmm nitems+1... curious. I had a code snippet which crashed and changing tp_alloc(type, 0) to tp_alloc(type, 1) fixed it. Will see if I can reproduce it again and post a minimal example.

casperdcl added a commit to AMYPAD/CuVec that referenced this pull request Mar 30, 2021
- `tp_alloc(T, n)` should allocation `n + 1` items python/cpython#24283 (comment)
@vstinner
Copy link
Member

Will see if I can reproduce it again and post a minimal example.

Open an issue at https://bugs.python.org/ in this case. In the meanwhile, I close your PR.

@vstinner vstinner closed this Mar 30, 2021
casperdcl added a commit to AMYPAD/CuVec that referenced this pull request Jul 20, 2021
- `tp_alloc(T, n)` should allocate `n + 1` items python/cpython#24283 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review docs Documentation in the Doc dir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants