all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Daniel Colascione <dancol@dancol.org>
To: "Aurélien Aptel" <aurelien.aptel+emacs@gmail.com>
Cc: Eli Zaretskii <eliz@gnu.org>, joakim@verona.se, emacs-devel@gnu.org
Subject: Re: FFI in Emacs
Date: Tue, 12 Mar 2013 17:31:11 -0700	[thread overview]
Message-ID: <513FC8CF.8030600@dancol.org> (raw)
In-Reply-To: <CA+5B0FMZHHF2S6eL=iFdEQ6_YWUq52zvyLPFCvJLaVYdyWoH3w@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2470 bytes --]

On 3/12/2013 3:53 PM, Aurélien Aptel wrote:
>>> a) a function can allocate memory that has to be freed
>>
>> At least on Windows, this cannot be done safely, so please don't
>> design the interface based on the assumption this is doable.  If a
>> shared object allocates memory, it should be responsible for freeing
>> it, or provide an API for telling it to free it.
> 
> I'm not sure I understand. You're saying that freeing a pointer
> returned by a library function which explicitly says you have to free
> it yourself is not safe on Windows? That seems strange.

Free using which heap? It's common for different modules in a Windows process to
use entirely different C runtime libraries. On Windows, all inter-module
dependencies are (module, symbol) pairs (written "module!symbol"), not just bare
symbol names as on ELF systems. That is, modA!fun1 calling modB!fun2 can exist
in the same process as a modC!fun1 calling modD!fun2. One module's malloc isn't
necessarily (and often isn't) the same as another module's malloc. It's also
common for Windows programs to allocate memory off private heaps, although
that's discouraged these days for performance reasons.

There's nothing stopping a POSIXish program from using multiple heaps either,
but due to tradition and the way ELF dynamically binds symbols, you're likely to
see most parts of a typical more programs tend to just use the system libc. It's
good practice, though, on both Windows and POSIXish systems, to provide a
function resource-specific (or at least module-specific) deallocation functions.
It's just that if you don't follow this advice under Windows, heap corruption
shoots you in the face.

(Incidentally, one of the challenges of writing Python loadable modules for
Windows is that Python is written with the assumption that all parts of the
program share the same libc, so if foo.pyd isn't linked to precisely the same
version of the C runtime that python27.dll is linked to, things go wrong very
quickly.)

Sometimes Windows library specify that memory is allocated on the "process
heap". The process heap is provided by ntdll.dll and functions more like a
Posixish system's libc heap in that it's shared by all parts of a process; see
HeapAlloc and HeapFree. Likewise, you can't share file descriptors or FILE*
pointers between different modules under Windows, but you *can* share HANDLEs,
since HANDLEs are universal and managed by the kernel.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 260 bytes --]

  reply	other threads:[~2013-03-13  0:31 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-27 23:36 FFI in Emacs Aurélien Aptel
2012-06-27 23:47 ` Glenn Morris
2012-06-28  0:03   ` John Wiegley
2012-06-28  2:12     ` Glenn Morris
2012-06-28  3:43       ` Stefan Monnier
2012-06-28  4:51         ` John Wiegley
2012-06-28 15:27           ` Nix
2012-07-11  5:24         ` John Wiegley
2012-07-11  5:53           ` Helmut Eller
2012-07-11  6:43           ` Michael Albinus
2012-07-11 14:04             ` Burton Samograd
2012-07-11 14:07               ` Burton Samograd
2012-07-11  7:14           ` Stephen J. Turnbull
2012-07-11  7:44             ` Michael Albinus
2012-07-11 16:11             ` Eli Zaretskii
2012-07-11 17:08               ` Samuel Bronson
2012-07-11 17:49                 ` Eli Zaretskii
2012-07-11 18:23                   ` Stephen J. Turnbull
2012-07-11 18:30                     ` Eli Zaretskii
2012-07-11 16:05           ` Eli Zaretskii
2012-07-12 13:55           ` Stefan Monnier
2012-07-12 17:01             ` Samuel Bronson
2012-07-12 18:54             ` John Wiegley
2012-07-12 23:19             ` Sam Steingold
2012-07-13  2:20               ` Óscar Fuentes
2012-07-13  3:21                 ` Stephen J. Turnbull
2012-06-28  6:20       ` joakim
2012-07-11  9:40         ` joakim
2012-08-25 17:25           ` Aurélien Aptel
2012-08-25 18:20             ` joakim
2012-08-26  8:57               ` Ivan Kanis
2012-08-26 13:11                 ` joakim
2013-03-12 19:16             ` Aurélien Aptel
2013-03-12 21:50               ` Eli Zaretskii
2013-03-12 22:40                 ` Pascal J. Bourguignon
2013-03-13  7:41                   ` Andreas Schwab
2013-03-12 22:53                 ` Aurélien Aptel
2013-03-13  0:31                   ` Daniel Colascione [this message]
2013-03-13  3:55                   ` Eli Zaretskii
2013-03-22 19:44               ` BT Templeton
2013-03-23  1:51                 ` Leo Liu
2013-03-23  2:04                   ` Noah Lavine
2013-03-23 16:07                 ` Stefan Monnier
2013-03-23 18:33                   ` Yagnesh Raghava Yakkala

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=513FC8CF.8030600@dancol.org \
    --to=dancol@dancol.org \
    --cc=aurelien.aptel+emacs@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=joakim@verona.se \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.