Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

remove alloca() #1609

Merged
merged 1 commit into from

4 participants

Walter Bright Andrei Alexandrescu Andrej Mitrović Brad Roberts
Walter Bright
Owner

because alloca() is badly supported by many C compilers

Andrei Alexandrescu andralex merged commit c01cafa into from
Andrei Alexandrescu
Owner

merged

Andrej Mitrović
Collaborator

merged

Just a FYI github notifies people when it's merged. (from recently IIRC).

Andrei Alexandrescu
Owner

Finally! No more manual msg needed.

Brad Roberts
Owner

This change set seems like a net-negative. What C compiler has problems with it? I haven't seen any bug reports that suggest this code is failing. The pattern ought to be encapsulated as it's not the first place you've done this. That encapsulation starts to suggest that std::string is likely worth considering as it does this sort of thing already.

Walter Bright WalterBright deleted the branch
Walter Bright
Owner

The problem is that various C compilers require system-specific include files. Encapsulating it doesn't work too well, because in order to work in a function alloca() must be used in that function, rather than in some called function.

Walter Bright
Owner

@braddr: note line 14 in the diff, the __sun include.

Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Deleted user Unknown referenced this pull request from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 3, 2013
  1. Walter Bright

    remove alloca()

    WalterBright authored
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 19 deletions.
  1. +19 −19 src/mtype.c
38 src/mtype.c
View
@@ -11,12 +11,7 @@
#define __C99FEATURES__ 1 // Needed on Solaris for NaN and more
#define __USE_ISOC99 1 // so signbit() gets defined
-#if defined (__sun)
-#include <alloca.h>
-#endif
-
#include <math.h>
-
#include <stdio.h>
#include <assert.h>
#include <float.h>
@@ -2184,9 +2179,6 @@ Identifier *Type::getTypeInfoIdent(int internal)
{
// _init_10TypeInfo_%s
OutBuffer buf;
- Identifier *id;
- char *name;
- size_t len;
if (internal)
{ buf.writeByte(mangleChar[ty]);
@@ -2195,19 +2187,27 @@ Identifier *Type::getTypeInfoIdent(int internal)
}
else
toDecoBuffer(&buf);
- len = buf.offset;
- name = (char *)alloca(19 + sizeof(len) * 3 + len + 1);
+
+ size_t len = buf.offset;
buf.writeByte(0);
-#if TARGET_OSX
- // The LINKc will prepend the _
- sprintf(name, "D%dTypeInfo_%s6__initZ", 9 + len, buf.data);
-#else
+
+ // Allocate buffer on stack, fail over to using malloc()
+ char namebuf[40];
+ size_t namelen = 19 + sizeof(len) * 3 + len + 1;
+ char *name = namelen <= sizeof(namebuf) ? namebuf : (char *)malloc(namelen);
+ assert(name);
+
sprintf(name, "_D%dTypeInfo_%s6__initZ", 9 + len, buf.data);
-#endif
- if (global.params.isWindows && !global.params.is64bit)
- name++; // C mangling will add it back in
//printf("name = %s\n", name);
- id = Lexer::idPool(name);
+ assert(strlen(name) < namelen); // don't overflow the buffer
+
+ size_t off = 0;
+ if (global.params.isOSX || global.params.isWindows && !global.params.is64bit)
+ ++off; // C mangling will add '_' back in
+ Identifier *id = Lexer::idPool(name + off);
+
+ if (name != namebuf)
+ free(name);
return id;
}
@@ -5354,7 +5354,7 @@ void TypeFunction::toDecoBuffer(OutBuffer *buf, int flag)
Parameter::argsToDecoBuffer(buf, parameters);
//if (buf->data[buf->offset - 1] == '@') halt();
buf->writeByte('Z' - varargs); // mark end of arg list
- if (next != NULL)
+ if(next != NULL)
next->toDecoBuffer(buf);
inuse--;
}
Something went wrong with that request. Please try again.