Function: cons

cons is a function defined in alloc.c.

Signature

(cons CAR CDR)

Documentation

Create a new cons, give it CAR and CDR as components, and return it.

Other relevant functions are documented in the list group.

View in manual

Probably introduced at or before Emacs version 15.

Shortdoc

;; list
(cons 1 '(2 3 4))
    => (1 2 3 4)

Source Code

// Defined in /usr/src/emacs/src/alloc.c
{
  register Lisp_Object val;

  if (cons_free_list)
    {
      ASAN_UNPOISON_CONS (cons_free_list);
      XSETCONS (val, cons_free_list);
      cons_free_list = cons_free_list->u.s.u.chain;
    }
  else
    {
      if (cons_block_index == CONS_BLOCK_SIZE)
	{
	  struct cons_block *new
	    = lisp_align_malloc (sizeof *new, MEM_TYPE_CONS);
	  memset (new->gcmarkbits, 0, sizeof new->gcmarkbits);
	  ASAN_POISON_CONS_BLOCK (new);
	  new->next = cons_block;
	  cons_block = new;
	  cons_block_index = 0;
	}
      ASAN_UNPOISON_CONS (&cons_block->conses[cons_block_index]);
      XSETCONS (val, &cons_block->conses[cons_block_index]);
      cons_block_index++;
    }

  XSETCAR (val, car);
  XSETCDR (val, cdr);
  eassert (!XCONS_MARKED_P (XCONS (val)));
  consing_until_gc -= sizeof (struct Lisp_Cons);
  cons_cells_consed++;
  return val;
}