all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Dmitry Antipov <dmantipov@yandex.ru>
Cc: Stefan Monnier <monnier@IRO.UMontreal.CA>,
	Emacs development discussions <emacs-devel@gnu.org>
Subject: Re: Benchmarking temporary Lisp objects [Was: Re: [RFC] temporary Lisp_Strings]
Date: Thu, 04 Sep 2014 09:03:47 -0700	[thread overview]
Message-ID: <54088D63.8010406@cs.ucla.edu> (raw)
In-Reply-To: <54087B5E.10402@yandex.ru>

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

Dmitry Antipov wrote:
> I don't know how to use VLAs in the way similar to alloca

For vectors the macro needs to generate a series of declarations and 
statements rather than an expression.  It's less clean, but it's good 
enough.  Something like the attached, say.

[-- Attachment #2: safe-vla.c --]
[-- Type: text/x-csrc, Size: 1609 bytes --]

/* Placeholders for what's in lisp.h already.  */

#include <stddef.h>
#include <stdbool.h>
#include <inttypes.h>

typedef long Lisp_Object;
enum { word_size = sizeof (Lisp_Object) };
enum { MAX_ALLOCA = 16 * 1024 };
#define USE_SAFE_ALLOCA bool sa_must_free = false
#define SAFE_FREE() (sa_must_free ? magic_freer () : (void) 0)

#define min(a, b) ((a) < (b) ? (a) : (b))

extern void *xnmalloc (size_t, size_t);
extern Lisp_Object make_save_memory (Lisp_Object *, ptrdiff_t);
extern void magic_freer (void);
extern void record_unwind_protect (void (*) (Lisp_Object), Lisp_Object);
extern void free_save_value (Lisp_Object);
extern _Noreturn void memory_full (size_t);


/* The new macro.  */

#ifdef __STDC_NO_VLA__
# define SAFE_LISP_ARRAY(name, elems)	\
    Lisp_Object *name;			\
    SAFE_ALLOCA_LISP (name, elems)
#else
# define SAFE_LISP_ARRAY(name, elems)					\
    ptrdiff_t name##n = elems;						\
    bool name##issmall = name##n <= MAX_ALLOCA / word_size;		\
    Lisp_Object name##vec[name##issmall && name##n ? name##n : 1];	\
    Lisp_Object *name;							\
    if (name##issmall)							\
      name = name##vec;							\
    else								\
      {									\
	name = xnmalloc (name##n, word_size);				\
	record_unwind_protect (free_save_value,				\
			       make_save_memory (name, name##n));	\
	sa_must_free = true;						\
      }
#endif


/* Example use.  */

Lisp_Object
foo (ptrdiff_t n)
{
  USE_SAFE_ALLOCA;
  SAFE_LISP_ARRAY (foo, n);
  for (ptrdiff_t i = 0; i < n; i++)
    foo[i] = i;
  Lisp_Object x = 0;
  for (ptrdiff_t i = 0; i < n; i++)
    x ^= foo[0];
  SAFE_FREE ();
  return x;
}

  reply	other threads:[~2014-09-04 16:03 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-02 12:55 [RFC] temporary Lisp_Strings Dmitry Antipov
2014-09-02 13:21 ` Andreas Schwab
2014-09-02 13:47   ` Dmitry Antipov
2014-09-02 14:14     ` Andreas Schwab
2014-09-02 15:00     ` Eli Zaretskii
2014-09-02 14:00 ` Stefan Monnier
2014-09-02 15:13   ` Dmitry Antipov
2014-09-02 17:32     ` Stefan Monnier
2014-09-03 10:23       ` Benchmarking temporary Lisp objects [Was: Re: [RFC] temporary Lisp_Strings] Dmitry Antipov
2014-09-03 13:14         ` Stefan Monnier
2014-09-03 14:39         ` Paul Eggert
2014-09-03 15:39           ` Dmitry Antipov
2014-09-03 16:42             ` Paul Eggert
2014-09-03 17:47               ` Stefan Monnier
2014-09-03 18:00                 ` Paul Eggert
2014-09-04  4:59               ` Dmitry Antipov
2014-09-04  5:13                 ` Paul Eggert
2014-09-04  5:51                   ` Dmitry Antipov
2014-09-04  6:45                     ` Paul Eggert
2014-09-04 13:11                     ` Stefan Monnier
2014-09-04 13:37                       ` Dmitry Antipov
2014-09-04 14:46                         ` Dmitry Antipov
2014-09-04 16:03                           ` Paul Eggert [this message]
2014-09-05  4:00                             ` Dmitry Antipov
2014-09-05  4:24                               ` Stefan Monnier
2014-09-05  9:28                                 ` Dmitry Antipov
2014-09-05  7:15                               ` Paul Eggert
2014-09-05  9:16                                 ` Dmitry Antipov
2014-09-05 14:35                                   ` Paul Eggert
2014-09-05 15:35                                 ` Stefan Monnier
2014-09-08 10:33                                   ` Dmitry Antipov
2014-09-08 12:01                                     ` Dmitry Antipov
2014-09-08 13:30                                       ` Stefan Monnier
2014-09-08 12:44                                     ` Stefan Monnier
2014-09-08 13:30                                       ` Stefan Monnier
2014-09-02 14:37 ` [RFC] temporary Lisp_Strings Paul Eggert
2014-09-02 15:24   ` Dmitry Antipov
2014-09-02 15:28   ` Dmitry Antipov

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=54088D63.8010406@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=dmantipov@yandex.ru \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@IRO.UMontreal.CA \
    /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.