src/audio/disk/SDL_diskaudio.c
author Sam Lantinga
Thu, 16 May 2013 00:43:22 -0700
changeset 7180 3733e68edbc3
parent 7038 7f22b9ba218f
child 7366 38c6f26bb010
permissions -rw-r--r--
Fixed bug 1846 - _allmul implementation in SDL_stdlib.c doesn't clean up the stack

Colin Barrett

I see this manifest itself (VS2012 x86) as:

"Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."

in the first call to SDL_GetTicks in my application. The disassembly at the problem line is:

hires_now.QuadPart *= 1000;
00AD0792 push 0
00AD0794 push 3E8h
00AD0799 mov eax,dword ptr [ebp-10h]
00AD079C push eax
00AD079D mov ecx,dword ptr [hires_now]
00AD07A0 push ecx
00AD07A1 call _allmul (0AE7D40h)
00AD07A6 mov dword ptr [hires_now],eax
00AD07A9 mov dword ptr [ebp-10h],edx

Apparently _allmul should be popping the stack but isn't (other similar functions in SDL_stdlib.c - _alldiv and whatnot - DO pop the stack).

A 'ret 10h' at the end of _allmul appears to do the trick
     1 /*
     2   Simple DirectMedia Layer
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 #include "SDL_config.h"
    22 
    23 #if SDL_AUDIO_DRIVER_DISK
    24 
    25 /* Output raw audio data to a file. */
    26 
    27 #if HAVE_STDIO_H
    28 #include <stdio.h>
    29 #endif
    30 
    31 #include "SDL_rwops.h"
    32 #include "SDL_timer.h"
    33 #include "SDL_audio.h"
    34 #include "../SDL_audiomem.h"
    35 #include "../SDL_audio_c.h"
    36 #include "SDL_diskaudio.h"
    37 
    38 /* environment variables and defaults. */
    39 #define DISKENVR_OUTFILE         "SDL_DISKAUDIOFILE"
    40 #define DISKDEFAULT_OUTFILE      "sdlaudio.raw"
    41 #define DISKENVR_WRITEDELAY      "SDL_DISKAUDIODELAY"
    42 #define DISKDEFAULT_WRITEDELAY   150
    43 
    44 static const char *
    45 DISKAUD_GetOutputFilename(const char *devname)
    46 {
    47     if (devname == NULL) {
    48         devname = SDL_getenv(DISKENVR_OUTFILE);
    49         if (devname == NULL) {
    50             devname = DISKDEFAULT_OUTFILE;
    51         }
    52     }
    53     return devname;
    54 }
    55 
    56 /* This function waits until it is possible to write a full sound buffer */
    57 static void
    58 DISKAUD_WaitDevice(_THIS)
    59 {
    60     SDL_Delay(this->hidden->write_delay);
    61 }
    62 
    63 static void
    64 DISKAUD_PlayDevice(_THIS)
    65 {
    66     size_t written;
    67 
    68     /* Write the audio data */
    69     written = SDL_RWwrite(this->hidden->output,
    70                           this->hidden->mixbuf, 1, this->hidden->mixlen);
    71 
    72     /* If we couldn't write, assume fatal error for now */
    73     if (written != this->hidden->mixlen) {
    74         this->enabled = 0;
    75     }
    76 #ifdef DEBUG_AUDIO
    77     fprintf(stderr, "Wrote %d bytes of audio data\n", written);
    78 #endif
    79 }
    80 
    81 static Uint8 *
    82 DISKAUD_GetDeviceBuf(_THIS)
    83 {
    84     return (this->hidden->mixbuf);
    85 }
    86 
    87 static void
    88 DISKAUD_CloseDevice(_THIS)
    89 {
    90     if (this->hidden != NULL) {
    91         if (this->hidden->mixbuf != NULL) {
    92             SDL_FreeAudioMem(this->hidden->mixbuf);
    93             this->hidden->mixbuf = NULL;
    94         }
    95         if (this->hidden->output != NULL) {
    96             SDL_RWclose(this->hidden->output);
    97             this->hidden->output = NULL;
    98         }
    99         SDL_free(this->hidden);
   100         this->hidden = NULL;
   101     }
   102 }
   103 
   104 static int
   105 DISKAUD_OpenDevice(_THIS, const char *devname, int iscapture)
   106 {
   107     const char *envr = SDL_getenv(DISKENVR_WRITEDELAY);
   108     const char *fname = DISKAUD_GetOutputFilename(devname);
   109 
   110     this->hidden = (struct SDL_PrivateAudioData *)
   111         SDL_malloc(sizeof(*this->hidden));
   112     if (this->hidden == NULL) {
   113         return SDL_OutOfMemory();
   114     }
   115     SDL_memset(this->hidden, 0, sizeof(*this->hidden));
   116 
   117     /* Open the audio device */
   118     this->hidden->output = SDL_RWFromFile(fname, "wb");
   119     if (this->hidden->output == NULL) {
   120         DISKAUD_CloseDevice(this);
   121         return -1;
   122     }
   123 
   124     /* Allocate mixing buffer */
   125     this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
   126     if (this->hidden->mixbuf == NULL) {
   127         DISKAUD_CloseDevice(this);
   128         return -1;
   129     }
   130     SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
   131 
   132     this->hidden->mixlen = this->spec.size;
   133     this->hidden->write_delay =
   134         (envr) ? SDL_atoi(envr) : DISKDEFAULT_WRITEDELAY;
   135 
   136 #if HAVE_STDIO_H
   137     fprintf(stderr,
   138             "WARNING: You are using the SDL disk writer audio driver!\n"
   139             " Writing to file [%s].\n", fname);
   140 #endif
   141 
   142     /* We're ready to rock and roll. :-) */
   143     return 0;
   144 }
   145 
   146 static int
   147 DISKAUD_Init(SDL_AudioDriverImpl * impl)
   148 {
   149     /* Set the function pointers */
   150     impl->OpenDevice = DISKAUD_OpenDevice;
   151     impl->WaitDevice = DISKAUD_WaitDevice;
   152     impl->PlayDevice = DISKAUD_PlayDevice;
   153     impl->GetDeviceBuf = DISKAUD_GetDeviceBuf;
   154     impl->CloseDevice = DISKAUD_CloseDevice;
   155 
   156     return 1;   /* this audio target is available. */
   157 }
   158 
   159 AudioBootStrap DISKAUD_bootstrap = {
   160     "disk", "direct-to-disk audio", DISKAUD_Init, 1
   161 };
   162 
   163 #endif /* SDL_AUDIO_DRIVER_DISK */
   164 
   165 /* vi: set ts=4 sw=4 expandtab: */