python


c mmap write long to mmaped-area


i want to write a python-c-extension which should write a list of integers into a ram-area because my first version with python was a little bit to slow (30 ms).
In python this perfectly works with following code:
with open("/dev/mem", "r+b") as f: # open file
ddr_mem = mmap.mmap(f.fileno(), PRU_ICSS_LEN, offset=PRU_ICSS) # map pru shared-ram
while offset < ramSize:
ddr_mem[(sharedRam+offset):(sharedRam+offset+4)] = struct.pack('i', self.data[self.i])
offset += 4
self.i += 1
Because in the list are only long integer values (4 Bytes) the offset will be increased by 4 for every new value in the list.
As the mmap-area is 12 kB big it is possible to write 3072 values into it, because 12288 Byte / 4 Byte = 3072 or i am wrong?
Now within my c extension i tried the same with this piece of code:
if(ddrMem == NULL) {
//printf("\nopen: shared-ram...");
mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
if (mem_fd < 0) {
printf("Failed to open /dev/mem (%s)\n", strerror(errno));
return NULL;
}
/* map the DDR memory */
ddrMem = mmap(0, 0x0FFFFFFF, PROT_WRITE | PROT_READ, MAP_SHARED, mem_fd, PRU_ICSS + OFFSET_DDR + 0xE000); //TODO: weird offset - 0xE000
if (ddrMem == NULL) {
printf("Failed to map the device (%s)\n", strerror(errno));
close(mem_fd);
return NULL;
}
for (i = 0; i < d_len; i++) {
PyObject* temp = PySequence_Fast_GET_ITEM(seq, i);
elem = PyInt_AsLong(temp);
*((long*) DDR_regaddr+offset) = elem; // write to shared ram
offset = offset + 4;
if(offset >= (ramSize)){
offset = 0;
}
But now only every fourth address in the ram-area will get a new value. If i increase the offset with one it works but then i am able to write twice the data --> 6144 elements.
What is the trick ? What i am doing wrong ? Are theses calulations right ? I am not sure if i am right with my thougts.

Your offset is incorrect because you are performing pointer arithmetic, which already accounts for the size of the long in question.
Try incrementing your pointer by 1 instead of 4.
long *ptr = 0xff;
long *ptrOffset = ptr + 1; // Will access the next 'long' space in memory
long *ptrOffset2 = ptr + 4; // Will access the fourth next 'long' space in memory
Also, the size of the long is actually architecture and compiler dependent. Assuming that it is 4 bytes is not safe.

Ok sorry.. :(
Wrong memory mapping !
-> missed an offset of 0xF000
But many thanks #kevr - this was also very helpful :)


Related Links

strangely added character to my strings when using .encode(“utf-8”) [duplicate]
How to remove invisible elements in SVG files using Pyhon
Is there any benefit to using Py_DECREF instead of Py_XDECREF for Python C Extensions?
How to store data from html form to postgres database using Django 1.10?
When reading strings from a .txt file '\n' is automatically placed after string. Can I remove this? [closed]
Indentation Error from Heroku for this Python bot. I cannot see?
Error: NoReverseMatch at / (after modifying code to include Bootstrap)
In django how to move objects from a sessions foreign key to a user foreign key when the user logs in
Python ctypes cannot get long string value returned from .so file
Adding rows to a Pandas dataframe based on a list in a column (and vice versa)
Repeat fields in django forms
Copy Redshift table from S3 csv file using Python?
Saving BytesIO to Django ImageField
Flask App will not load app.py (The file/path provided (app) does not appear to exist)
How do I send HTML Formatted emails, through the gmail-api for python
DoesNotExist: [Model] matching query does not exist at id (ObjectId)

Categories

HOME
sharepoint
semantic-ui
erlang
rotation
system-verilog
ipython
boost-thread
otrs
zend-framework2
dataframe
powershell-v3.0
maxima
squarespace
event-log
lenskit
aptana
plupload
flexboxgrid
sql-update
continuous-deployment
skmaps
hash-collision
dss
classpath
invoke-command
pep8-assembly
roundcube
lmdb
helix-3d-toolkit
canvasjs
hashcat
vsts-package-management
dql
brute-force
tic-tac-toe
polyfills
intellij-idea-2016
ical-dotnet
g1gc
pytest-django
stress-testing
scalafx
rdw
jtds
mbaas
fedora20
httplib2
castle-dynamicproxy
nsfetchedresultscontrolle
visible
image-editing
spring-lemon
spring-repositories
financial
usb-drive
plone-3.x
schtasks.exe
retro-computing
android-snackbar
futuretask
anti-patterns
risk-analysis
xjc
subversion-edge
google-plus-one
hiera
c++-actor-framework
power-law
angulartics
convex-polygon
entity-framework-4.1
ng-pattern
fraud-prevention
tridion2009
mozart-mvc
cadisplaylink
fotoware
pstree
z-machine
point-sprites
ubuntu-11.04
wchar
ajax.beginform
zookeeper
nemerle
coff
authenticode
windows-live-id
file-encodings
jquery-ui-button
front-controller
soft-keyboard
castle-validators
dotproject





Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm