Skip to content

Commit f3de572

Browse files
committed
pretty much done with 2.0.0
1 parent 4393cc3 commit f3de572

18 files changed

+586
-79
lines changed

README.md

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,22 @@ Why would you ever need this
1212

1313
### Examples
1414

15-
```py
16-
from pointers import Pointer, decay
17-
18-
a: str = '123'
19-
b: str = 'abc'
20-
21-
@decay
22-
def move(ptr_a: Pointer[str], ptr_b: Pointer[str]):
23-
ptr_a <<= ptr_b
24-
25-
move(a, b)
26-
print(a, b) # abc abc
27-
```
28-
2915
```py
3016
from pointers import _
3117

32-
ptr = _&"hello world" # creates a new pointer object
33-
assert _*ptr == "hello world"
18+
text: str = "hello world"
19+
ptr = _&text # creates a new pointer object
20+
ptr <<= "world hello"
21+
print(text) # world hello
3422
```
3523

3624
```py
37-
from pointers import fopen, fprintf, fclose
25+
from pointers import c_malloc as malloc, c_free as free, strcpy, printf
3826

39-
file = fopen("/dev/null", "w") # assigns to the c FILE* type
40-
fprintf(file, "hello world")
41-
fclose(file)
27+
ptr = malloc(3)
28+
strcpy(ptr, "hi")
29+
printf("%s\n", ptr)
30+
free(ptr)
4231
```
4332

4433
### Features

docs/allocation.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Allocation
2+
3+
## Basics
4+
5+
We can use memory functions (`malloc`, `free`, `calloc`, `realloc`) the same way you would use them in C, and use them via the pointer API.
6+
7+
Here's an example:
8+
9+
```py
10+
from pointers import malloc, free
11+
12+
ptr = malloc(28) # 28 is the size of integers larger than 0
13+
free(ptr)
14+
```
15+
16+
We can dereference the same way we did earlier, but first, we need to actually put something in the memory. We can do this via data movement:
17+
18+
```py
19+
from pointers import malloc, free
20+
21+
ptr = malloc(28)
22+
ptr <<= 1
23+
print(*ptr)
24+
free(ptr)
25+
```
26+
27+
Data movement is much safer when using memory allocation, since we aren't actually overwriting memory tracked by Python.
28+
29+
We also aren't overwriting any existing objects, we are just putting the object into a memory space.
30+
31+
Here's a quick example:
32+
33+
```py
34+
from pointers import malloc, free
35+
36+
ptr = malloc(28)
37+
ptr <<= 1
38+
print(*ptr)
39+
ptr <<= 2
40+
print(*ptr, 1) # prints out "2 1", since we dont have to overwrite the 1 object itself!
41+
free(ptr)
42+
```
43+
44+
We can bypass size limits the same way as before, but again, this is extremely discouraged. Instead, we should use `realloc`.
45+
46+
## Reallocation
47+
48+
The `realloc` function works a bit differently in pointers.py. We don't reassign the pointer like you would in C:
49+
50+
```py
51+
ptr = realloc(ptr, 28)
52+
```
53+
54+
Instead, we can just call `realloc` on the object directly, like so:
55+
56+
```py
57+
from pointers import malloc, realloc, free
58+
59+
ptr = malloc(24)
60+
ptr <<= 0
61+
realloc(ptr, 28)
62+
free(ptr)
63+
```
64+
65+
## Identity
66+
67+
Identity of objects in CPython are defined by their memory address, so using `is` on objects inside allocated memory won't work properly:
68+
69+
```py
70+
from pointers import malloc, free
71+
import sys
72+
73+
text: str = "hello world"
74+
ptr = malloc(sys.getsizeof(text))
75+
ptr <<= text
76+
print(~ptr is text) # False
77+
```
78+
79+
## Arrays
80+
81+
We can allocate an array using `calloc`:
82+
83+
```py
84+
from pointers import calloc, free
85+
86+
ptr = calloc(4, 28) # allocate an array of 4 slots of size 28
87+
```
88+
89+
You can (somewhat) use an allocated array as you would in C:
90+
91+
```py
92+
from pointers import calloc, free
93+
94+
array = calloc(4, 28)
95+
96+
for index, ptr in enumerate(array):
97+
ptr <<= index
98+
99+
print(ptr[1]) # prints out "1"
100+
```

0 commit comments

Comments
 (0)